0

我有以下主程序:

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 个线程。

提前致谢。

4

1 回答 1

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");

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

希望这可以帮助!

于 2013-02-07T23:16:39.987 回答