Difference between mutex lock and pthread_join
What's the difference between the two?
Aren't they the same thing in that they both wait for a thread to finish before executing another thread?
I'm trying to understand the following code
#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开发者_如何学Go=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(0);
}
void *functionC()
{
pthread_mutex_lock( &mutex1 );
counter++;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
}
Thanks.
They're not actually the same thing.
A mutex (mutual exclusion semaphore) is a means to restrict the use of a resource to one thread at a time (with both threads obviously capable of running). When a thread returns successfully from a pthread_mutex_lock
call, it is guaranteed to be the only thread holding that lock. Any thread that tries to lock that mutex after that point will generally have to wait until the owning thread unlocks it.
In other words, the thread with the lock is the only thread capable of manipulating the resource protected by that lock (assuming, of course, that other threads don't touch the resource without first acquiring the lock - you have to play by the rules).
A pthread_join
, on the other hand, allows a thread to wait for another thread to exit. This is often used in the main thread to wait for all child threads to exit (there are other uses, that's just a typical one). A successful return from pthread_join
means that the other thread is no longer running.
In the code you've shown, two threads are running concurrently and both the counter
increment and the call to printf
are protected by mutex1
. The pthread_join
calls at the end of main
will make the main thread wait until your two child threads exit before continuing.
As an aside, you should check the return values from pthread_mutex_lock
since it can fail. You don't want to go ahead and modify the protected resource in that case as corruption may occur. Ditto for pthread_join
.
And, for a more comprehensive test, the following function would be better:
void *functionC() {
int i;
for (i = 1000; i > 0; i--) {
pthread_mutex_lock (&mutex1);
counter++;
printf ("Counter value: %d\n", counter);
pthread_mutex_unlock (&mutex1);
}
}
since it will more likely have the threads running side-by-side. Without the loop, there's a good chance one thread will exit before the second even starts.
pthread_join()
waits for a thread to exit. A mutex_lock acquires control of a semaphore which prevents cooperating threads from simultaneously accessing the protected resource.
精彩评论