2017-10-13 92 views
0

我寫了我的第一個多線程程序,並且大部分程序都能正常工作。共享緩衝區數組最初填充爲-1,指示生產者其空並需要被填充。然後生產者用隨機值1 - 10填充共享緩衝區,生產者輪流填充緩衝區。生產者然後向消費者發出信號,告知消費者它已經填充了緩衝區的一個元素並消耗它。生產者需要填寫120個元素,每個消費者都應該填寫。該程序工作完全正常,直到它到達項目110.它然後凍結,我不知道爲什麼。我該如何解決?多線程程序在完成之前就凍結

這是輸出的一個片段。

Item: 85, Consuming value 8, my thread id is: 1216 
Item: 86, Consuming value 7, my thread id is: 298320 
Signal 
Producer thread 231296 and value: 0 
Producer thread 297552 and value: 2 
Producer thread 298576 and value: 0 
Item: 87, Consuming value 9, my thread id is: 297808 
Signal 
Producer thread 960 and value: 3 
Producer thread 298064 and value: 2 
Item: 88, Consuming value 3, my thread id is: 231744 
Item: 89, Consuming value 7, my thread id is: 298320 
Item: 90, Consuming value 3, my thread id is: 1216 
Item: 91, Consuming value 7, my thread id is: 298832 
Signal 
Producer thread 231296 and value: 3 
Producer thread 297552 and value: 8 
Producer thread 298576 and value: 6 
Item: 92, Consuming value 2, my thread id is: 297808 
Signal 
Producer thread 960 and value: 9 
Producer thread 298064 and value: 7 
Item: 93, Consuming value 5, my thread id is: 298320 
Item: 94, Consuming value 2, my thread id is: 298832 
Item: 95, Consuming value 0, my thread id is: 1216 
Item: 96, Consuming value 2, my thread id is: 231744 

這是我的代碼

#include <stdio.h> 
#include <string.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <stdint.h> 


#define THREADS 5 
#define ELEMENTS 120 


pthread_t tid_producer[THREADS], tid_consumer[THREADS]; 
int value = 0; 
int saveValue = 0; 
void *produce(void *arg); 
void *consume(void *arg); 
int producerCount =0; 
int consumerCount = ELEMENTS; 

struct { 
    pthread_mutex_t mutex; 
    int index; 
    int value; 
    int MyShBuff[ELEMENTS]; 
} add = {PTHREAD_MUTEX_INITIALIZER, 0, 0}; 


    struct{ 
    pthread_mutex_t mutex; 
    pthread_cond_t  cond; 
    int nready; 
    int value; 
    int empty; 
    int counter; 

    /* number ready for consumer */ 
} nready = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,0, -2, ELEMENTS}; 

int main() 
{ 

    int i, j, k; 

    //Ready buffer for producers 
    for (i =0; i < ELEMENTS; i++) 
    { 
     add.MyShBuff[i]=-1; 
    } 



    for(j = 0; j < THREADS; j++) { 

    pthread_create(&tid_producer[j], NULL, &produce, NULL); 
    pthread_create(&tid_consumer[j], NULL, &consume, NULL); 
} 



/* wait for all producers and the consumer*/ 

    for(k = 0; k < THREADS; k++) { 
     pthread_join(tid_producer[k], NULL); 
     pthread_join(tid_consumer[k], NULL);  
    } 

    /* Clean up and exit */ 

    pthread_mutex_destroy(&nready.mutex); 
    pthread_mutex_destroy(&add.mutex); 
    pthread_cond_destroy(&nready.cond); 
    pthread_exit(NULL); 

    exit(0);  
    return 0; 
} 

void *produce(void *arg) 
    { 
    int i = 0; 


    for (; ;) 
    { 
     pthread_mutex_lock(&add.mutex); 
     if(add.index >= ELEMENTS) 
     { 

      pthread_mutex_unlock(&add.mutex); 
      return NULL; 
     } 
     if(add.MyShBuff[add.index] == -1) 
     { 
     add.value = rand() % 10 + 0; 
     add.MyShBuff[add.index] = add.value; 
     printf("Producer thread %d and value: %d\n" ,pthread_self(), add.MyShBuff[add.index]); 
     add.index++; 
     } 
     pthread_mutex_unlock(&add.mutex); 
     pthread_mutex_lock(&nready.mutex); 

     if(nready.nready == 0) 
     { 
     pthread_cond_broadcast(&nready.cond); 
     printf("Signal\n"); 
     } 

     nready.nready++; 
     pthread_mutex_unlock(&nready.mutex); 

     } 


    } 


void *consume(void *arg) 
{ 

pthread_mutex_lock(&nready.mutex); 

    while(nready.empty != 0) 
    { 

    while (nready.nready == 0) 
    { 


     pthread_cond_wait(&nready.cond,&nready.mutex); 


     pthread_mutex_lock(&add.mutex); 
     printf(" Item: %d, Consuming value %d, my thread id is: %d\n", nready.counter, add.MyShBuff[nready.counter], pthread_self()); 
     add.MyShBuff[nready.counter] = -2; 
     pthread_mutex_unlock(&add.mutex); 

     nready.counter++; 
     nready.empty--; 


    } 

    nready.nready--; 
    pthread_mutex_unlock(&nready.mutex); 
    } 


    return NULL; 


} 
+1

儘量減少THREADS調試1。目前,你的程序凍結。 – miradham

+1

除了Miradham所說的內容外,在''consume'中你鎖定'nready.mutex'一次,但是在外部循環的每次迭代中解鎖它,這不可能是正確的。 –

回答

1

正如在評論中指出,使用互斥的是錯誤的消耗(想象而conditon是錯誤的,該線程將離開不釋放互斥)此外 ,我會建議限制持有相同信息的變量的數量(空,計數器),這使流程難以遵循。 最後,您的pthread_wait_cond中還存在一個錯誤: 請參閱Does pthread_cond_wait(&cond_t, &mutex); unlock and then lock the mutex?或解釋pthread_wait_cond使用的任何其他問題:他們強調返回時謂詞MAY爲真,這意味着您必須在開始之前再次測試它需要謂詞爲真的計算。

我重寫了一下你的代碼以使其工作;我希望我沒有錯過別的東西,但可以幫助你走的更遠:如果消費進入生產後等待互斥做廣播

#include <stdio.h> 
#include <string.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <stdint.h> 


#define THREADS 5 
#define ELEMENTS 120 


pthread_t tid_producer[THREADS], tid_consumer[THREADS]; 
int value = 0; 
int saveValue = 0; 
void *produce (void *arg); 
void *consume (void *arg); 
int producerCount = 0; 
int consumerCount = ELEMENTS; 

struct 
{ 
    pthread_mutex_t mutex; 
    int index; 
    int value; 
    int MyShBuff[ELEMENTS]; 
} add = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; 


struct 
{ 
    pthread_mutex_t mutex; 
    pthread_cond_t cond; 
    int nready; 
    int value; 
    int empty; 
    int counter; 

    /* number ready for consumer */ 
} nready = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, -2, ELEMENTS, 0 }; 

int main(int argc, char **argv) 
{ 
    //Ready buffer for producers 
    for (int i = 0; i < ELEMENTS; i++) { 
     add.MyShBuff[i] = -1; 
    } 


    for (int j = 0; j < THREADS; j++) { 
     pthread_create (&tid_producer[j], NULL, &produce, NULL); 
     pthread_create (&tid_consumer[j], NULL, &consume, NULL); 
    } 

    /* wait for all producers and the consumer */ 
    for (int k = 0; k < THREADS; k++) { 
     pthread_join (tid_producer[k], NULL); 
     pthread_join (tid_consumer[k], NULL); 
    } 

    /* Clean up and exit */ 
    pthread_mutex_destroy (&nready.mutex); 
    pthread_mutex_destroy (&add.mutex); 
    pthread_cond_destroy (&nready.cond); 

    return 0; 
} 

void *produce(void *dummy) 
{ 
    pthread_mutex_lock (&add.mutex); 

    while (add.index < ELEMENTS) { 
     if (add.MyShBuff[add.index] == -1) 
     { 
      add.value = rand() % 10 + 0; 
      add.MyShBuff[add.index] = add.value; 
      printf("Producer thread %ld and value: %d\n" ,pthread_self(), add.MyShBuff[add.index]); 
      add.index++; 
     } 
     pthread_mutex_unlock (&add.mutex); 

     pthread_mutex_lock (&nready.mutex); 
     { 
      if (nready.nready == 0) 
      { 
       pthread_cond_broadcast (&nready.cond); 
       printf ("Signal\n"); 
      } 
      nready.nready++; 
     } 
     pthread_mutex_unlock (&nready.mutex); 

     pthread_mutex_lock (&add.mutex); 
    } 

    pthread_mutex_unlock (&add.mutex); 

    return NULL; 
} 


void *consume(void *dummy) 
{ 
    pthread_mutex_lock (&nready.mutex); 

    while (nready.empty != 0) 
    { 
     // you also need to check it is not time to leave... 
     while (nready.nready == 0 && nready.empty != 0) { 
      pthread_cond_wait (&nready.cond, &nready.mutex); 
     } 

     if (nready.empty == 0) { 
      break; 
     } 

     pthread_mutex_lock (&add.mutex); 
     printf(" Item: %d, Consuming value %d, my thread id is: %ld\n", nready.counter, add.MyShBuff[nready.counter], pthread_self()); 
     add.MyShBuff[nready.counter] = -2; 

     pthread_mutex_unlock (&add.mutex); 

     nready.counter++; 
     nready.empty--; 

     nready.nready--; 
    } 

    pthread_mutex_unlock (&nready.mutex); 

    return NULL; 
}