Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While accessing shared data inside multi threaded applications it is always very important to protect shared data. In your case you have two threads accessing the standard output simultaneously, therefore you have a race condition wherein two threads race to acquire <code>stdout</code>. Therefore I've taken the liberty to adapt you program in to a case where the standard output is protected by a mutex. Just before the the stdout is used we lock a mutex, the other thread will also try to lock the mutex, but both can't do that in the same time simultaneously. Thus the 2nd thread that tries to lock is put to sleep.</p> <p>And as others have told you it might seem that the program you originally wrote made nice outputs, that more or less coincidence, on other systems there is no way you can guarantee that and the output of the two threads can mix up. ending in output like</p> <pre><code>1256734 8 5671234 8 </code></pre> <p>etc</p> <p>Now you might think that once the mutex is unlocked the other will automatically take over, but that is up to the scheduler. Therefore in the example below the program might do several 1234 before outputting 5678. But it will always be either 1234 or 5678 and those outputs aren't mixed. If you really want to alternate the two thread you have to more sophisticated output and lookup conditional variable for example, pthread API supports those as wel.</p> <pre><code>#include&lt;pthread.h&gt; #include&lt;stdio.h&gt; void *print1(void *arg) { pthread_mutex_t* lock = arg; while(1) { /*now we do output from one thread while we lock the *mutex from the main function.*/ pthread_mutex_lock(lock); printf("1"); printf("2"); printf("3"); printf("4\n"); fflush(stdout); /*now we must lock the lock, because otherwise *the other thread can't lock the mutex and if *we would try to lock the mutex again we would *create a deadlock, hower recursive locking is *possible, but beyond the scope of my anwer.*/ pthread_mutex_unlock(lock); } return NULL; } void *print2(void *arg) { /*see the note in the frist thread*/ pthread_mutex_t* lock = arg; while(1){ pthread_mutex_lock(lock); printf("5"); printf("6"); printf("7"); printf("8\n"); fflush(stdout); pthread_mutex_unlock(lock); } return NULL; } int main() { pthread_t tid1, tid2; /*lets create a lock to protect our shared resources *in this case the standard output. */ pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; /*make sure that the mutex is available inside the thread *by sending a pointer to the mutex as fourth argument to *phtread_create.*/ pthread_create(&amp;tid1, NULL, print1, &amp;lock); pthread_create(&amp;tid2, NULL, print2, &amp;lock); pthread_join(tid1, NULL); pthread_join(tid2, NULL); return 0; } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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