2013-02-25 50 views
2

我正在使用互斥試圖限制對某個線程的代碼的某些部分的訪問,但而不是鎖定一次,並阻止其他線程,它似乎允許所有線程「鎖定」。以下是我的代碼,然後顯示代碼不工作的輸出部分。PThread互斥不作爲希望

//headers defined, etc 

pthread_mutex_t queuemutex = PTHREAD_MUTEX_INITIALIZER; 

// other code with various functions 

int main(void) { 

    //unrelated code 

    threadinformation **threadArray = (threadinformation **)malloc(POOLSIZE * sizeof(threadinformation)); 

    int k; 
    for (k = 0; k < POOLSIZE; k++) { 
     pthread_t thread; 
     threadinformation *currentThread = (threadinformation *)malloc(sizeof(threadinformation)); 
     currentThread->state = (int *)malloc(sizeof(int)); 
     currentThread->state[0] = 0; 
     currentThread->currentWaiting = currentWaiting; 
     currentThread->number = k; 
     threadArray[k] = currentThread; 
     pthread_create(&thread, NULL, readWriteToClient, threadArray[k]); //thread is created here 
     currentThread->thread = thread; 
     joinArray[k] = thread; 
    } 

    //unrelated code 

} 

static void* readWriteToClient(void *inputcontent) { 

    while(1){ 

     //unrelated code 

     pthread_mutex_lock(&queuemutex); //problem happens here 

     fprintf(stderr,"Thread %d got locked \n",threadInput->number); 

     while((threadInput->currentWaiting->status) == 0){ 
      pthread_cond_wait(&cond, &queuemutex); 
      fprintf(stderr,"Thread %d got signalled \n",threadInput->number); 
     } 

     connfd = threadInput->currentWaiting->fd; 
     threadInput->currentWaiting->status = 0; 
     pthread_cond_signal(&conncond); 
     pthread_mutex_unlock(&queuemutex); 

     //unrelated code 

    } 

} 

輸出。

Thread 0 got locked 
Thread 7 got locked 
Thread 25 got locked 
Thread 97 got locked 
Thread 6 got locked 
Thread 5 got locked 
Thread 4 got locked 
Thread 3 got locked 
Thread 8 got locked 
Thread 9 got locked 
Thread 10 got locked 
Thread 11 got locked 
Thread 12 got locked 
Thread 13 got locked 
Thread 14 got locked 
Thread 15 got locked 
Thread 16 got locked 
Thread 17 got locked 
Thread 18 got locked 
Thread 19 got locked 
Thread 20 got locked 
    And so on... 
+0

Threadinput-> currentwaiting-> status聲明爲volatile嗎? – 2013-02-26 00:50:20

+0

你應該考慮刪除那個問題,現在對方得到了答案! – didierc 2013-02-26 13:21:37

回答

3

沒有問題。

pthread_cond_wait(& cond,& queuemutex);

等待條件變量釋放互斥量。

+0

同樣重要的是,它在pthread_cond_wait返回時重新使用它 – nos 2013-12-12 21:26:07

1

什麼fceller說更長的版本是,調用pthread_cond_wait(& COND,&互斥)做了三件事在返回之前:它釋放鎖,然後等待一個信號,然後等待(如有必要)重新獲得鎖定。

您的示例沒有顯示發送第一個信號的內容(如果有的話),並且不會顯示工作線程正在等待的狀態(如果有的話)。

這是pthread_cond_wait()和pthread_cond_signal()的更典型用例。

void producer() { 
    pthread_mutex_lock(&mutex); 
    push_something_on_the_queue(); 
    ptherad_cond_signal(&cond); 
    pthread_mutex_unlock(&mutex); 
} 

void consumer() { 
    pthread_mutex_lock(&mutex); 
    while (! time_to_quit) { 
     if (queue_is_empty) { 
      pthread_cond_wait(&cond, &mutex); 
     } else { 
      thing = take_something_from_the_queue(); 

      pthread_mutex_unlock(&mutex); 
      do_something_with(thing); 
      pthread_mutex_lock(&mutex); 
     } 
    } 
    pthread_mutex_unlock(&mutex); 
} 

生產者線程將東西放入隊列中。消費者線程等待事件出現在隊列中,然後將它們彈出並對它們做些什麼。消費者在一個循環中等待,每次喚醒時它都會檢查隊列的狀態(即使隊列中沒有任何東西,線程可能會「虛假地」發出信號)。

當消費者正在檢查隊列時,互斥鎖被鎖定,並且它將保持鎖定狀態,直到消費者休眠或將某事從隊列中彈出。當消費者正在睡覺時,互斥鎖是而不是,當消費者正在做某件事時,它不會被鎖定。

任何數量的消費者可以同時在consumer()方法中。他們可能在不同的事情上工作,或者他們可能在睡覺;但是互斥體確保了在任何給定時間只有一個線程(生產者或消費者)可以觸摸隊列。