Thursday, 15 January 2015

c - Unusual error where thread is not getting created? -



c - Unusual error where thread is not getting created? -

i have next main program:

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 stock %d" , stock); printf("\n supply ies %d", supply); timeremaining--; if (stock + supply > cap_max) stock = cap_max; else stock = stock + supply; sleep(0.1); } exit(exit_success); }

ok well, 95% of time run programme error creating supply thread. , never prints hello. makes no sense. 1 thread.

thanks in advance.

you have semicolon after if statement:

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

this means statement appears nested within if statement not nested @ , execute regardless of condition. specifically, c interpreting code mean

if (pthread_create(&supplierid, null, &supplier, null) != 0) ; /* nil */ error("error creating supply threads \n");

to prepare this, remove stray semicolon.

hope helps!

c pthreads

No comments:

Post a Comment