c - pthread programming, threads don't run simultaneously -


i've following code in programm:

for (i = 0; < numthrs; i++) {       if (0 != pthread_create(&thrs[i], null, thr_main,                 &args[i]))     {         /* handle error */     }      printf("thread %d created.\n", i);      if (0 != pthread_join(thrs[i], &thread_status))     {         /* handle error */     }      printf("joined thread %d\n", i); } 

but i've noticed threads don't run simultaneously, second thread starts execution after first thread has completed execution. i'm new pthread programming. can tell me what's proper way start threads simultaneously?

this because pthread_join each thread after creating it, i.e. waiting thread n complete prior starting thread n+1.

instead, try:

for (i = 0; < numthrs; i++) {     if (0 != pthread_create(&thrs[i], null, thr_main,                 &args[i]))     {         /* handle error */     }      printf("thread %d created.\n", i); }  (i = 0; < numthrs; i++) {     if (0 != pthread_join(thrs[i], &thread_status))     {         /* handle error */     }      printf("joined thread %d\n", i); } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -