amazingsaluyot
amazingsaluyot

Reputation: 47

Pthread Mutex Lock Linux

I created a simple program that shows the use of mutex lock. Here is the code...

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_THREAD 2

pthread_mutex_t mutex;
int call_time;

void *makeCall(void *param)
{
    call_time = 10;

    pthread_mutex_lock(&mutex);

        printf("Hi I'm thread #%u making a call\n", (unsigned int) pthread_self());
        do{
            printf("%d\n", call_time);
            call_time--;
            sleep(1);
        }
        while(call_time > 0);

    pthread_mutex_unlock(&mutex);

    return 0;
}

int main()
{

    int i; 
    pthread_t thread[NUM_THREAD];
    //init mutex
    pthread_mutex_init(&mutex, NULL);
    //create thread
    for(i = 0; i < NUM_THREAD; i++)
        pthread_create(&thread[i], NULL, makeCall, NULL);
    //join thread
    for(i = 0; i < NUM_THREAD; i++)
        pthread_join(thread[i], NULL);

    pthread_mutex_destroy(&mutex);
    return 0;
}

The output is...

Hi I'm thread #3404384000 making a call
10
10
9
8  
7
6
5
4
3
2
1
Hi I'm thread #3412776704 making a call
0

However, if I modify the function makeCall and transfer the variable call_time inside the mutex locks...

pthread_mutex_lock(&mutex);
    call_time = 10;
    /*
     * 
     *
     *
     */
pthread_mutex_unlock(&mutex);

The program now gives me the correct output where each of the thread counts down from 10 to 0. I don't understand the difference it makes transferring the variable call_time inside the locks. I hope someone can make me understand this behavior of my program. Cheers!

Upvotes: 0

Views: 708

Answers (1)

Baruch
Baruch

Reputation: 21538

call_time is a shared variable that is accessed from 2 threads and so must be protected. What is happening is that the first thread starts, sets call_time to 10 and prints the first round.Then the second thread starts, resets call_time back to 10 and waits for the mutex. The first thread now comes back and keeps running with call_time reset to 10. After it is done and frees the mutex, the second thread can now run. call_time is now 0 since the first thread left it at 0, and so it just prints the last round.

Try this program, I think it will demonstrate threads better:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define NUM_THREAD 2

pthread_mutex_t mutex;
int call_time;

void *makeCall(void *param)
{
    int temp;
    do{
        pthread_mutex_lock(&mutex);
        printf("Hi I'm thread #%u making a call\n", (unsigned int) pthread_self());
        printf("%d\n", call_time);
        temp = call_time--;
        pthread_mutex_unlock(&mutex);
        //sleep(1); //try with and without this line and see the difference.
    }
    while(temp > 0);

    return 0;
}

int main()
{
    int i; 
    call_time = 100;
    pthread_t thread[NUM_THREAD];
    //init mutex
    pthread_mutex_init(&mutex, NULL);
    //create thread
    for(i = 0; i < NUM_THREAD; i++)
        pthread_create(&thread[i], NULL, makeCall, NULL);
    //join thread
    for(i = 0; i < NUM_THREAD; i++)
        pthread_join(thread[i], NULL);

    pthread_mutex_destroy(&mutex);
    return 0;
}

Upvotes: 1

Related Questions