【问题标题】:pthread_join skipping first threadpthread_join 跳过第一个线程
【发布时间】:2011-02-17 19:25:56
【问题描述】:

所以我有以下代码,其中一些省略了以便更容易理解。

for (unsigned int t = 0; t < NUM_THREADS; t++)
    {

        if (pthread_create(&threads[t], NULL, thread_run, (void*) &threadData) != 0)
        {
            perror("pthread_create");
        }//end if
    }

    for (unsigned int z = 0; z < NUM_THREADS; z++)
    {
        if (pthread_join(threads[z], NULL) != 0)
        {
            perror("pthread_join");
        }
    }

我的问题是连接函数,它跳过了我创建的第一个线程,然后继续。我目前的解决方案是添加一个额外的线程,而不是让第一个线程做任何工作。

任何想法为什么会发生这种情况?

【问题讨论】:

    标签: multithreading join pthreads


    【解决方案1】:

    IMO 没有 pthreads 问题;您只是在创建 NUM_THREADS + 1 线程并仅加入其中的第一个 NUM_THREADS

    【讨论】:

    • 奥普斯;那不应该在那里,用于我的问题解决方案,我现在将更新代码。
    • @Pieces 所以你说pthread_join(threads[0]) 不等待threads[0] 就返回并且没有错误返回?这反而表明它在连接循环触发时已经终止。
    • 不是因为需要写出的数据不存在。
    • @Pices 线程可以随时杀死自己,留下 NULL 或只是错误的指针。我只是确定这个问题存在于代码的其他部分,而不是这个创建和连接循环。
    • @mbq,这是什么常见原因?泰
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    相关资源
    最近更新 更多