我有以下主程序:
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/