2011-10-04 35 views
4

有一個程序,我就這一工作後,我啓動它,工作了一段時間,然後攤位。下面是該程序的簡化版本:並行線程程序運行一段時間,然後攤

#include <cstdlib> 
#include <iostream> 
#include <pthread.h> 

pthread_t* thread_handles; 
pthread_mutex_t mutex; 
pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER; 
int thread_count; 
const int some_count = 77; 
const int numb_count = 5; 
int countR = 0; 

//Initialize threads 
void InitTh(char* arg[]){ 
    /* Get number of threads */ 
    thread_count = strtol(arg[1], NULL, 10); 
    /*Allocate space for threads*/ 
    thread_handles =(pthread_t*) malloc (thread_count*sizeof(pthread_t)); 
} 

//Terminate threads 
void TermTh(){ 
    for(long thread = 0; thread < thread_count; thread++) 
     pthread_join(thread_handles[thread], NULL); 
    free(thread_handles); 
} 

void* DO_WORK(void* replica) { 
    /*Does something*/ 
    pthread_mutex_lock(&mutex); 
    countR++; 
    if (countR == numb_count) pthread_cond_broadcast(&cond_var); 
    pthread_mutex_unlock(&mutex); 
} 

//Some function 
void FUNCTION(){ 
    pthread_mutex_init(&mutex, NULL); 
    for(int k = 0; k < some_count; k++){ 
     for(int j = 0; j < numb_count; j++){ 
      long thread = (long) j % thread_count; 
      pthread_create(&thread_handles[thread], NULL, DO_WORK, (void *)j);; 
     } 
     /*Wait for threads to finish their jobs*/ 
     pthread_mutex_lock(&mutex); 
     if (countR < numb_count) while(pthread_cond_wait(&cond_var,&mutex) != 0); 
     countR = 0; 
     pthread_mutex_unlock(&mutex); 
     /*Does more work*/ 
    } 
    pthread_cond_destroy(&cond_var); 
    pthread_mutex_destroy(&mutex); 
} 


int main(int argc, char* argv[]) {  
    /*Initialize threads*/ 
    InitTh(argv); 

    /*Do some work*/ 
    FUNCTION(); 

    /*Treminate threads*/ 
    TermTh(); 

    return 0; 
} 

some_count,(在我的具體情況,)小於76,則程序工作正常,但如果我指定一個較大的值的程序,如前面提到的,工作一段時間然後停止。也許有人可以指出我做錯了什麼?

+2

什麼是你通常在運行時使用的輸入?我的意思是Thread_count的價值是什麼? –

+0

從二到四。 –

+2

那麼你如何期望在你的malloc二到四時同時運行5個線程(j循環)? –

回答

2

我想你應該初始化線程數numb_count而不是argv
然後更換

long thread = (long) j % thread_count; 

long thread = (long) j; 

會不知道它解決它,但它需要反正...

此外,它不是關於數字76或77,你有線程使用中的競爭條件。 讓說,你一個線程到了點「DO_WORK」當他解開互斥,但他仍然沒有從這個函數返回(表示線程仍在運行...)。那麼你可以嘗試使用下一次迭代中創建相同的線程:

pthread_create(&thread_handles[thread], NULL, DO_WORK, (void *)j); 

固定,變化:

pthread_mutex_lock(&mutex); 
    if (countR < numb_count) while(pthread_cond_wait(&cond_var,&mutex) != 0); 
    countR = 0; 
    pthread_mutex_unlock(&mutex); 

到:

pthread_mutex_lock(&mutex); 
    if (countR < numb_count) while(pthread_cond_wait(&cond_var,&mutex) != 0); 
    countR = 0; 
    for(long thread = 0; thread < numb_count; thread++) 
     pthread_join(thread_handles[thread], NULL); 
    pthread_mutex_unlock(&mutex); 
+0

非常感謝它的幫助下,我的意思是,現在的方案致力於通過更大量的循環,但仍最終檔可能還有另外一個問題在這裏再次感謝它。是非常有幫助! –

+0

你應該(如果你沒有的話)刪除for循環從TermTH()或程序會卡住正在等待中,而沒有線程運行一個線程來完成他的工作... –

+0

詩如果問題已解決請選擇答案作爲「答案」,這樣它將接近於防止他人浪費時間來解決它。 –

3

 long thread = (long) j % thread_count; 
     pthread_create(&thread_handles[thread], NULL, DO_WORK, (void *)j);; 

可以「覆蓋」初始化線程句柄,這取決於你的實際線程數參數。

1

你可以嘗試使用helgrind來分析它。

Valgrind的安裝,然後啓動的valgrind --tool = helgrind yourproject,看看helgrind吐出

1

你既不正確初始化您的互斥量(這裏未導致錯誤),也不會存儲您正確地創建線程。試試這個:

for(int count = 0; count < thread_count; ++count) { 
    pthread_create(&thread_handles[count], NULL, DO_WORK, (void *)(count % numb_count)); 
} 
相關問題