pthread_join没有等待线程完成

时间:2018-06-16 21:36:49

标签: c pthreads pthreads-win32

我在Windows上使用pthreads.h,用于简单的光线跟踪器。似乎主要功能不是等待线程完成。当我像这样运行程序时(我现在简化了它,只测试线程,但它仍然给出了错误):

typedef struct {
    unsigned int id; 
} Thread_Data;

void* render_band(void* arg) {
    Thread_Data* data = (Thread_Data*)arg;
    printf("This is thread number %d", data->id);
    pthread_exit(0);
}

int main() {
    pthread_t threads[NUM_THREADS];
    Thread_Data data[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; i++) {
        data[i].id = id;

        int rc = pthread_create(&threads[i], NULL, render_band, &data[i]);
        if (rc) {
            printf("[ERROR] From pthread_create: %d\n", rc);
        }
    } 

    for (int i = 0; i < NUM_THREADS; i++) {
        int rc = pthread_join(threads[i], NULL);
        if (rc) {
            printf("[ERROR] From pthread_join: %d\n", rc);
        }
    }
}

图像将无法完成,只会渲染几个像素。 然而,当我添加睡眠时,图像确实完成了。让我相信pthread_join不会等待,即使文档说明了这一点。我在这里缺少什么?

编辑:添加错误检查,它返回pthread_join的错误代码3。

1 个答案:

答案 0 :(得分:0)

我很遗憾浪费了别人的时间。这个问题最终成了图书馆本身。当我在Windows上寻找pthreads.h时,我在sourceforge上找到了一些库。但显然MinGW已经获得了pthread支持,并且它搞砸了一切。因此对于具有相同问题的人,只需使用MinGW附带的那个。