0

The following code is taken from this site and it shows how to use mutexes. It implements both pthread_join and pthread_mutex_lock:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int  counter = 0;

main()
{
   int rc1, rc2;
   pthread_t thread1, thread2;

   /* Create independent threads each of which will execute functionC */

   if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
   {
      printf("Thread creation failed: %d\n", rc1);
   }

   if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )
   {
      printf("Thread creation failed: %d\n", rc2);
   }

   /* Wait till threads are complete before main continues. Unless we  */
   /* wait we run the risk of executing an exit which will terminate   */
   /* the process and all threads before the threads have completed.   */

   pthread_join( thread1, NULL);
   pthread_join( thread2, NULL); 

   exit(EXIT_SUCCESS);
}

void *functionC()
{
   pthread_mutex_lock( &mutex1 );
   counter++;
   printf("Counter value: %d\n",counter);
   pthread_mutex_unlock( &mutex1 );
}

I ran the code as given above as it is and it produced following result:

Counter value: 1

Counter value: 2

But in the second run i removed "pthread_mutex_lock( &mutex1 );" and "pthread_mutex_unlock( &mutex1 );" . I compiled and ran the code, it again produced the same result.

Now the thing that confuses me is why mutex lock is used in above code when same thing can be done without it (using pthread_join)? If pthread_join prevents another thread from running untill the first one has finished then i think it would already prevent the other thread from accessing the counter value. Whats the purpose of pthread_mutex_lock?

Qandeel Abbassi
  • 999
  • 7
  • 31

1 Answers1

5

The join prevents the starting thread from running (and thus terminating the process) until thread1 and thread2 finish. It doesn't provide any synchronization between thread1 and thread2. The mutex prevents thread1 from reading the counter while thread2 is modifying it, or vice versa.

Without the mutex, the most obvious thing that could go wrong is that thread1 and thread2 run in perfect synch. They each read zero from the counter, each add one to it, and each output "Counter value: 1".

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • But they never run in perfect synch because one thread has to wait untill the other thread finishes with pthread_join...i have tested...even if the threads are in larger quantity they never run into each other just by using pthread_join. Are there some conditions in which threads can access the resources at the same time even if pthread_join is called? – Qandeel Abbassi Oct 02 '15 at 17:20
  • @QandeelAbbasi Only the starting thread calls `pthread_join`. Neither thread1 nor thread2 call it. The only thread that ever waits for a thread to finish is the starting thread. Also, both threads could run to completion before `pthread_join` is even called. – David Schwartz Oct 02 '15 at 17:20
  • @DavidSchwartz, What do you mean by "starting thread" here? Is it the main function? – Dr. Essen Apr 04 '18 at 12:11
  • @Ac3_DeXt3R The starting thread is the thread that calls the `main` function. – David Schwartz Apr 21 '18 at 23:25
  • @DavidSchwartz, so basically it's the main process when we run an executable? – Dr. Essen Apr 25 '18 at 03:58
  • @Ac3_DeXt3R No. There is no "main process", there's just one process. When a process is created, one thread is also created. Some people call that the "main thread", but that leads to confusion because they think it means the thread is in some sense primary rather than just the thread that invokes `main`. – David Schwartz Apr 26 '18 at 00:31
  • @DavidSchwartz Ok, I think I understood. Thanks. :) – Dr. Essen Apr 27 '18 at 08:49