pthreads 程序运行一段时间然后停止

发布于 2024-12-08 03:07:11 字数 1930 浏览 0 评论 0原文

我正在开发一个程序,启动后它会运行一段时间然后停止。这是该程序的简化版本:

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

There is a program I am working on that, after I launch it, works for some time and then stalls. Here is a simplified version of the program:

#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;
}

When some_count, (in my particular case,) is less than 76, the program works fine, but if I specify a larger value the program, as mentioned earlier, works for some time and then stalls. Maybe somebody can point out what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你对谁都笑 2024-12-15 03:07:11

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

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

In

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

you can "override" initialized thread handles, depending on your actual thread count parameter.

_畞蕅 2024-12-15 03:07:11

我认为你应该将线程号初始化为 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);

I think you should init the thread number to numb_count rather then argv
then replace

 long thread = (long) j % thread_count;

with

 long thread = (long) j;

won't sure it fix it, but it's needed anyway...

Moreover, it's not about the number 76 or 77, you have a race condition in the thread use.
lets say that one of you threads got to the point in "DO_WORK" when he unlock the mutex but he still didn't returned from this function (meaning the thread is still running...). then you may try to create the same thread in the next iteration using:

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

fixing, change:

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

to:

    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);
残花月 2024-12-15 03:07:11

你可以尝试使用helgrind来分析它。

安装 valgrind,然后启动 valgrind --tool=helgrind yourproject 并查看 helgrind 会输出什么

You could try to analyze it using helgrind.

Install valgrind, then launch valgrind --tool=helgrind yourproject and see what helgrind spits out

笑,眼淚并存 2024-12-15 03:07:11

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

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

You are neither initializing your mutex correctly (not causing the error here), nor storing the threads you create correctly. Try this:

for(int count = 0; count < thread_count; ++count) {
    pthread_create(&thread_handles[count], NULL, DO_WORK, (void *)(count % numb_count));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文