2015-04-23 56 views
0

我繼續昨天的幫助,並且在子線程中添加了另一個代碼。基本上,當用戶輸入stdin時,子線程應該讀取它並將其返回給父線程。但是,打印完輸出後,應將代碼重定向回到子線程並等待用戶按Enter鍵,一旦用戶按下Enter鍵,代碼應退出。這是工作,但我已經使用睡眠(),我只想使用互斥體(),當我評論sleep(),然後下面的代碼首先打印(「按回車」),然後父代碼打印實際輸入。如何使用c編程中的子線程中的互斥處理同步

/*Required header files are added*/ 
#include <stdio.h> 
#include <pthread.h> 
#include <stdlib.h> 
#include <unistd.h> 
/*this structure will hold the string of user input and the lock variable has created*/ 
struct thread_main 
{ 
    char *buffer; 
    char *bufferparent; 
    pthread_mutex_t lock; 
    pthread_mutex_t lock1; 

} td; 

/*it is a child thread, and store the user value in a buffer variable which has been declared in the thread_main structure*/ 


static void *thread(void *buff) 
    { 
    /*the pointer has assigned to the structure, so we can get the values of a buffer array*/  
     struct thread_arguments *arg = buff; 

     //the previous code 

     pthread_mutex_unlock(&td.lock); 
     pthread_mutex_destroy(&td.lock); 

     sleep(1); // I want to ignore this. 
     pthread_mutex_init(&td.lock, 0); 
     pthread_mutex_lock(&td.lock); 

     printf("press enter"); 
     /*this code will read buffer and check the enter*/ 

     pthread_mutex_unlock(&td.lock); 
     pthread_mutex_destroy(&td.lock); 

     return NULL; 
    } 
+2

上面的代碼看起來不像C++ –

+3

線程函數中'arg'的大小是指針的大小;顯然,您正在64位機器上使用32位機器或32位版本。您可能需要使用指針和大小創建一個結構,並將指向該結構的指針傳遞給線程函數。 –

+0

@LưuVĩnhPhúc該標籤已被刪除 –

回答

1

正如我在評論指出:

arg在線程功能的大小是一個指針的大小;顯然,您正在64位機器上使用32位機器或32位版本。您可能需要使用指針和大小創建一個結構,並將指向該結構的指針傳遞給線程函數。

此代碼的工作,並說明我的意思:

#include <stdio.h> 
#include <pthread.h> 

struct thread_data 
{ 
    char *buffer; 
    pthread_mutex_t lock; 
} td; 

struct thread_arg 
{ 
    char *buffer; 
    size_t buflen; 
}; 

static void *thread(void *data) 
{ 
    struct thread_arg *arg = data; 

    printf("%zd\n", arg->buflen); 
    td.buffer = fgets(arg->buffer, arg->buflen, stdin); 

    pthread_mutex_unlock(&td.lock); 

    return NULL; 
} 

int main(void) 
{ 
    char buffer[128]; 
    struct thread_arg arg = { buffer, sizeof(buffer) }; 
    pthread_t thread_id; 

    pthread_mutex_init(&td.lock, 0); 
    pthread_mutex_lock(&td.lock); 
    printf("Enter Sync Command -- "); 

    pthread_create(&thread_id, NULL, thread, &arg); 

    pthread_mutex_lock(&td.lock); 
    printf("message read by parent- %s", td.buffer); 

    pthread_join(thread_id, NULL); 

    pthread_mutex_unlock(&td.lock); 
    pthread_mutex_destroy(&td.lock); 
    return 0; 
} 

需要注意的是,如果他們是一樣的原鎖則不能使用鎖的副本。該代碼不檢查fgets()實際上是否返回一行數據(一個錯誤)。

我不相信鎖是必要的。 我不相信struct thread_data值得保留。此代碼也可以工作,依賴於父級嘗試讀取緩衝區之前退出的線程。

#include <stdio.h> 
#include <pthread.h> 

struct thread_arg 
{ 
    char *buffer; 
    size_t buflen; 
}; 

static void *thread(void *data) 
{ 
    struct thread_arg *arg = data; 

    printf("%zd\n", arg->buflen); 
    fgets(arg->buffer, arg->buflen, stdin); 

    return NULL; 
} 

int main(void) 
{ 
    char buffer[128]; 
    struct thread_arg arg = { buffer, sizeof(buffer) }; 
    pthread_t thread_id; 

    printf("Enter Sync Command -- "); 

    pthread_create(&thread_id, NULL, thread, &arg); 
    pthread_join(thread_id, NULL); 

    printf("message read by parent- %s", buffer); 
    return 0; 
} 

如果您的代碼和同步變得更復雜,那麼鎖定是個好主意。您可能希望將其添加到結構中(或等效地將緩衝區大小添加到結構中)。

+0

感謝您的回答,以及應用鎖功能的要求之一的任務,因此我不得不使用它。 –

2

原因sizeof(arg)將返回4個字節,就是你所要求的一個指針的大小,這將4個字節。您需要另一種方式來告訴線程您的數組長度。