Click to See Complete Forum and Search --> : creating multiple threads without waiting to finish


Endurance
March 2nd, 2008, 11:07 AM
Hello everyone,

Im trying to create 7 threads in a row, very simple. I want all threads to be created, even if the previous thread hasnt finished yet. I am using the <pthread.h> include. Here is the part of my code where I create the threads:


pthread_t tid1, tid2;
pthread_attr_t attr;
pthread_attr_init(&attr);


pthread_create(&tid1, NULL, Process1(), NULL); /*this works fine*/
pthread_create(&tid2, NULL, Process2(), NULL); /*doesnt get here*/


Code is written in C and compiled in unix using gcc and -lpthread switch.

Daniel

stephendoyle75
March 2nd, 2008, 03:10 PM
When you start tid1 it is likely that a thread context switch will occur and Process1() will start running. At some point the OS scehduler should determine that tid1 has got enough of the processor and then return control back to the main thread and tid2 should get created.

If you never get to tid2, then check to make sure that Process1() is ok and doesn't crash.

If you want to make sure that all threads are created before you let them off running, then use something like a condition or mutex to synchronize the startup process.

Endurance
March 2nd, 2008, 03:29 PM
Thanks, I figured it out in the meanwhile. I had to change


pthread_create(&tid1, NULL, Process1(), NULL);


to


pthread_create(&tid1, NULL, Process1, NULL);

exterminator
April 9th, 2008, 10:54 AM
Strange if that even compiled. You should have checked the return value of pthread_create to see if the thread creation was successful. You should have probably got an error but you ignored the return value. It would be 0 for success and otherwise some kind of error occured. Which error code, I am not sure but probably it would highlight the error. If not, you were just having bad luck as neither the compiler nor the library allowed you to spot the error.

See : http://opengroup.org/onlinepubs/007908799/xsh/pthread_create.html for various error codes and descriptions.