Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C pthread_join未等待线程完成_C_Pthreads_Pthreads Win32 - Fatal编程技术网

C pthread_join未等待线程完成

C pthread_join未等待线程完成,c,pthreads,pthreads-win32,C,Pthreads,Pthreads Win32,我在Windows上使用pthreads.h作为一个简单的光线跟踪器。看起来主函数并不是在等待线程完成。当我像这样运行程序时(我现在简化了它,只测试线程,但它仍然会给出错误): typedef结构{ 无符号整数id; }线程数据; void*渲染带(void*arg){ 线程数据*数据=(线程数据*)参数; printf(“这是线程编号%d”,数据->id); pthread_退出(0); } int main(){ pthread_t threads[NUM_threads]; 线程数据数据[

我在Windows上使用pthreads.h作为一个简单的光线跟踪器。看起来主函数并不是在等待线程完成。当我像这样运行程序时(我现在简化了它,只测试线程,但它仍然会给出错误):

typedef结构{
无符号整数id;
}线程数据;
void*渲染带(void*arg){
线程数据*数据=(线程数据*)参数;
printf(“这是线程编号%d”,数据->id);
pthread_退出(0);
}
int main(){
pthread_t threads[NUM_threads];
线程数据数据[线程数];
对于(inti=0;i
图像将不会完成,只渲染几个像素。 但是,当我添加睡眠时,图像确实完成了。这让我相信pthread_join不会等待,即使文档中这么说。我错过了什么


编辑:添加了错误检查,它返回pthread_连接的错误代码3。

很抱歉浪费了大家的时间。问题最终是图书馆本身。当我在Windows上查找pthreads.h时,我在sourceforge上找到了一些库。但很明显,MinGW已经有了pthread支持,它把一切都搞砸了。因此,对于有相同问题的人,只需使用MinGW附带的一个即可。

多大的
NUM_THREADS
?您应该在
pthread\u create
pthread\u join
调用中添加错误检查。您在调用
pthread\u create()
pthread\u join()
时缺少错误检查,因此您不知道哪些成功,哪些失败。是不是
height/NUM_THREADS
的计算结果为0(小图片,很多线程),所以线程与此无关?打印出每个线程应该执行的操作的详细信息—每个线程的
thread\u数据
条目—以确保它执行您想要的操作。我认为由于
minu_y
max_y
计算,很有可能会丢失很多像素。请参阅。您尝试连接的线程在连接点处似乎已不存在?你的
渲染带
是什么样子的?当你失败时,你可能应该打印
i
的值。是第一个线程(索引0)失败了吗?如果是这样,那么线程函数
render_band()
至少有可能访问
&data[i]
指针,以便在
pthread_t threads[NUM_threads]上进行写操作数组,从而减少了从
pthread\u join()
获取有用信息的机会。您得到的是一个错误还是多个错误?
data[i].id=id看起来不应该编译,因为
id
在任何地方都没有定义。你是说
data[i].id=i
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);
        }
    }
}