c - 未创建线程的异常错误?

标签 c pthreads

我有以下主程序:

int main(int argc, char** argv) {

    /*checkParameters(argc,argv);*/

    if (pthread_create(&supplierid, NULL, &supplier, NULL) != 0);
        error("ERROR creating supply threads \n");

}

void *supplier () {

    printf("hello? \n"); 

    while (timeremaining >= 0) {


        printf("\n the stock is %d" , stock);
        printf("\n the supply ies %d", supply);

        timeremaining--;

        if (stock + supply > cap_max)
            stock = cap_max;
        else
            stock = stock + supply;

        sleep(0.1);
    }

    exit(EXIT_SUCCESS);
}

好吧,在我运行这个程序的 95% 的时间里,我都遇到了创建供应线程的错误。它从不打印你好。 这是没有意义的。它只有 1 个线程。

提前致谢。

最佳答案

if 语句后有一个分号:

if (pthread_create(&supplierid, NULL, &supplier, NULL) != 0);

这意味着看似嵌套在 if 语句中的语句实际上根本没有嵌套,并且无论条件如何都将始终执行。具体来说,C 正在将您的代码解释为

if (pthread_create(&supplierid, NULL, &supplier, NULL) != 0)
    ; /* Do nothing */

error("ERROR creating supply threads \n");

要解决此问题,请删除杂散的分号。

希望这对您有所帮助!

关于c - 未创建线程的异常错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14762920/

相关文章:

c - undefined reference

c - TCP 连接断开检测(如果中间链路断开)?

c - 使用 mmap() 映射多个进程共享内存区域

c - 尝试释放时出现堆错误

c++ - 函数中的线程在 X 时间后执行另一个函数时出现问题

c - Linux 从 C 程序的 pthread 重启网络

c - 让这个 for 循环更加高效?

c - 分离的 pthread 导致内存泄漏

c - 使用多个线程时,C 中的多线程编程速度较慢

c++ - 范围互斥锁的自定义 RAII C++ 实现