我有以下主程序:
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个线程。
提前谢谢。答案 0 :(得分:3)
您的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");
要解决此问题,请删除分散的分号。
希望这有帮助!