Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After <code>pthread_join()</code> Add</p> <pre><code>/* Last thing that main() should do */ pthread_exit(NULL); </code></pre> <p>For more details pls refer <a href="https://computing.llnl.gov/tutorials/pthreads/" rel="nofollow">here</a>.</p> <p>pthread_join() is used to make the main() thread wait till all the threads completes it's execution. Control reaches the pthread_join(), when that particular thread completes it's execution. So if you try to cancel/kill that particular thread, error will be returned. </p> <p><strong>Sample Code</strong></p> <pre><code> #include &lt;pthread.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define NUM_THREADS 4 void *BusyWork(void *t) { int i; long tid; tid = (long)t; printf("Thread %ld starting...\n",tid); pthread_exit((void*) t); //Exits that current thread. } int main (int argc, char *argv[]) { pthread_t thread[NUM_THREADS]; int rc; long t; void *status; for(t=0; t&lt;NUM_THREADS; t++) { printf("Main: creating thread %ld\n", t); rc = pthread_create(&amp;thread[t], NULL, BusyWork, (void *)t); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } /* Wait for completion */ for(t=0; t&lt;NUM_THREADS; t++) { rc = pthread_join(thread[t], &amp;status); if (rc) { printf("ERROR; return code from pthread_join() is %d\n", rc); exit(-1); } printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status); } printf("Main: program completed. Exiting.\n"); pthread_exit(NULL); // Exits main thread. } </code></pre> <p>For @Joachim Pileborg</p> <pre><code>#include &lt;pthread.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define NUM_THREADS 4 void *BusyWork(void *t) { int i; long tid; tid = (long)t; printf("Thread %ld starting...\n",tid); for(i = 0; i &lt;10000000; i++) printf("\n Thread: %ld, %d", tid, i); pthread_exit((void*) t); //Exits that current thread. } int main (int argc, char *argv[]) { pthread_t thread[NUM_THREADS]; int rc; long t; void *status; for(t=0; t&lt;NUM_THREADS; t++) { printf("Main: creating thread %ld\n", t); rc = pthread_create(&amp;thread[t], NULL, BusyWork, (void *)t); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } //printf("Main: program completed. Exiting.\n"); pthread_exit(NULL); // Exits main thread(work will be done). //return 0;//( Threads will exit once main exits.) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload