pthreads程序工作一段时间后然后停止

时间:2011-10-04 07:18:04

标签: c++ multithreading pthreads

有一个我正在研究的程序,在我启动它之后,工作了一段时间然后停止。以下是该计划的简化版本:

#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时,程序运行正常,但如果我指定一个更大的值,程序,如前所述,工作一段时间然后停止。也许有人可以指出我做错了什么?

4 个答案:

答案 0 :(得分:3)

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

您可以“覆盖”初始化的线程句柄,具体取决于您的实际线程计数参数。

答案 1 :(得分: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);

答案 2 :(得分:1)

您可以尝试使用helgrind进行分析。

安装valgrind,然后启动valgrind --tool = helgrind yourproject并查看helgrind吐出的内容

答案 3 :(得分:1)

您既没有正确初始化互斥锁(这里没有导致错误),也没有正确存储您创建的线程。试试这个:

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