Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to suspend a thread before another thread finishes its work
    primarykey
    data
    text
    <p>hi there i'm working on POSIX threads and i have a work like</p> <ul> <li>We have 5 threads which each of them will print 'a','b','c','d,'e' separately with using same function</li> <li>Threads must be created in alphabetical order.</li> <li>Each character must be printed 50 times on screen</li> <li>'a' can only printed after all 'c's are printed</li> <li>'b' can only printed after all 'd's are printed</li> <li>'c' can only printed after all 'd's are printed</li> </ul> <p>so before i display my code i have to explain what i have done. first of all, i have 5 global variables (which are flags and all of them set to 1 initiallly ) and i assigned them to the value which pthread_create() function returns. and then in the main function i used if() conditions before i joined threads. but i guess i'm trying the wrong way. the issue is i coundnt handle with the process "before thread_a, thread_d must finish its work". i will appreciate if you can help and suggest some better ideas. and here is my code,</p> <pre><code>#include &lt;pthread.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; int counter = 0; int flag_a = 1; int flag_b = 1; int flag_c = 1; int flag_d = 1; int flag_e = 1; pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; void* thread_function(void* str); void print_char(char* ch); int main(){ pthread_t thread_a, thread_b, thread_c, thread_d, thread_e; char* a = "a"; char* b = "b"; char* c = "c"; char* d = "d"; char* e = "e"; flag_a = pthread_create( &amp;thread_a, NULL, thread_function, (void*) a ); flag_b = pthread_create( &amp;thread_b, NULL, thread_function, (void*) b ); flag_c = pthread_create( &amp;thread_c, NULL, thread_function, (void*) c ); flag_d = pthread_create( &amp;thread_d, NULL, thread_function, (void*) d ); flag_e = pthread_create( &amp;thread_e, NULL, thread_function, (void*) e ); if(flag_c==0) pthread_join( thread_a, NULL ); pthread_join( thread_b, NULL ); if(flag_d==0) pthread_join( thread_c, NULL ); if(flag_d==0) pthread_join( thread_d, NULL ); pthread_join( thread_e, NULL ); printf("\nCounter = %d\n", counter); return 0; } void* thread_function(void* str) { int i; char* msg = (char*) str; for(i=0; i&lt;50; i++) { pthread_mutex_lock(&amp;mymutex); counter++; print_char(msg); pthread_mutex_unlock(&amp;mymutex); } } void print_char(char* ch){ int i; for(i=0; i&lt;1; i++) { printf("%s --&gt; ", ch); fflush(stdout); sleep(1); } } </code></pre> <p>edit: i found a solution which prints the characters properly but the issue i cant print them asynchronousnly. i mean whenever i run the code it has same output like:</p> <p>e-->d-->c-->b-->a (every character printed at 50 times )</p> <p>and this the editted part of thread_function()</p> <pre><code>void* thread_function(void* str) { int i; char* msg = (char*) str; while( (strcmp(msg,"a") == 0) &amp;&amp; (counter_c &lt; 5) ){ ;; } while( (strcmp(msg,"c") == 0) &amp;&amp; (counter_d &lt; 5) ){ ;; } while( (strcmp(msg,"b") == 0) &amp;&amp; (counter_d &lt; 5) ){ ;; } for(i=0; i&lt;5; i++) { pthread_mutex_lock(&amp;mymutex); counter++; if( strcmp(msg,"d") == 0 ) counter_d++; if( strcmp(msg,"c") == 0 ) counter_c++; print_char(msg); pthread_mutex_unlock(&amp;mymutex); } } </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.
 

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