【问题标题】:Call join child pthread in main function在 main 函数中调用 join child pthread
【发布时间】:2015-12-07 11:07:22
【问题描述】:

我有测试代码:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

pthread_t th_worker, th_worker2;



void * worker2(void *data) {
    for(int i = 0; i< 1000000; i++){
            printf("thread for worker2----%d\n", i);
            usleep(500);
    }
}
void * worker(void *data){
    pthread_create(&th_worker2, NULL, worker2, data);

    for(int i = 0; i< 100; i++){
            printf("thread for worker-----%d\n", i);
            usleep(500);
    }
}
void join(pthread_t _th){
    pthread_join(_th, NULL);
}

在 main() 函数中,如果我调用 join(the_worker2):

int main() {
    char* str = "hello thread";

    pthread_create(&th_worker, NULL, worker, (void*) str);
    /* problem in here */
    join(th_worker2);

    return 1;
}

--> 段错误错误

否则,我打电话:

join(the_worker);
join(th_worker2);

--->好的

为什么在上述情况下会出现段错误? 感谢帮助!!!

【问题讨论】:

    标签: c++ c main pthread-join pthread-exit


    【解决方案1】:

    如果您发布了所有您的代码,则您有一个竞态条件

    mainworker 的开头同步,但 worker2

    也就是说,main 正在尝试加入th_worker2 之前 worker 有机会调用pthread_create 并设置th_worker2 有效 em> [非空] 值。

    所以,th_worker2 将在第二个 pthread_create 完成之前无效,但对于 main 来说已经太迟了。它已经获取了th_worker2,它的值为NULL,main 将出现段错误。

    当您为th_worker 添加联接时,它会起作用,因为它保证 同步和 竞争条件。


    要在没有连接的情况下实现此保证,请执行以下操作:

    int
    main()
    {
        char *str = "hello thread";
    
        pthread_create(&th_worker, NULL, worker, (void *) str);
    
        // give worker enough time to properly start worker2
        while (! th_worker2)
            usleep(100);
    
        /* problem in here */
        join(th_worker2);
    
        return 1;
    }
    

    一个更好的方法是添加一个额外的变量。有了这个,第一个循环就不需要了[但我把它留在了]:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    
    int worker_running;
    pthread_t th_worker;
    
    int worker2_running;
    pthread_t th_worker2;
    
    void *
    worker2(void *data)
    {
    
        // tell main we're fully functional
        worker2_running = 1;
    
        for (int i = 0; i < 1000000; i++) {
            printf("thread for worker2----%d\n", i);
            usleep(500);
        }
    
        return NULL;
    }
    
    void *
    worker(void *data)
    {
    
        // tell main we're fully functional
        worker_running = 1;
    
        pthread_create(&th_worker2, NULL, worker2, data);
    
        for (int i = 0; i < 100; i++) {
            printf("thread for worker-----%d\n", i);
            usleep(500);
        }
    
        return NULL;
    }
    
    void
    join(pthread_t _th)
    {
        pthread_join(_th, NULL);
    }
    
    int
    main()
    {
        char *str = "hello thread";
    
        pthread_create(&th_worker, NULL, worker, (void *) str);
    
        // give worker enough time to properly start worker2
        // NOTE: this not necessarily needed as loop below is better
        while (! th_worker2)
            usleep(100);
    
        // give worker2 enough time to completely start
        while (! worker2_running)
            usleep(100);
    
        /* problem in here (not anymore!) */
        join(th_worker2);
    
        return 1;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      相关资源
      最近更新 更多