≡ Menu

How to Use C Mutex Lock Examples for Linux Thread Synchronization

In the Linux threads series, we discussed on the ways in which a thread can terminate and how the return status is passed on from the terminating thread to its parent thread. In this article we will throw some light on an important aspect known as thread synchronization.

Linux Threads Series: part 1, part 2, part 3, part 4 (this article).

Thread Synchronization Problems

Lets take an example code to study synchronization problems :

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

pthread_t tid[2];
int counter;

void* doSomeThing(void *arg)
{
    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);

    for(i=0; i<(0xFFFFFFFF);i++);
    printf("\n Job %d finished\n", counter);

    return NULL;
}

int main(void)
{
    int i = 0;
    int err;

    while(i < 2)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

    return 0;
}

The above code is a simple one in which two threads(jobs) are created and in the start function of these threads, a counter is maintained through which user gets the logs about job number which is started and when it is completed. The code and the flow looks fine but when we see the output :

$ ./tgsthreads
Job 1 started
Job 2 started
Job 2 finished
Job 2 finished

If you focus on the last two logs, you will see that the log ‘Job 2 finished’ is repeated twice while no log for ‘Job 1 finished’ is seen.

Now, if you go back at the code and try to find any logical flaw, you’ll probably not find any flaw easily. But if you’ll have a closer look and visualize  the execution of the code, you’ll find that :

  • The log ‘Job 2 started’ is printed just after ‘Job 1 Started’  so it can easily be concluded that while thread 1 was processing the scheduler scheduled the thread 2.
  • If the above assumption was true then the value of the ‘counter’ variable got incremented again before job 1 got finished.
  • So, when Job 1 actually got finished, then the wrong value of counter produced the log ‘Job 2 finished’ followed by the ‘Job 2 finished’  for the actual job 2 or vice versa as it is dependent on scheduler.
  • So we see that its not the repetitive log but the wrong value of the ‘counter’ variable that is the problem.

The actual problem was the usage of the variable ‘counter’ by second thread when the first thread was using or about to use it. In other words we can say that lack of synchronization between the threads while using the shared resource ‘counter’ caused the problems or in one word we can say that this problem happened due to ‘Synchronization problem’ between two threads.

Mutexes

Now since we have understood the base problem, lets discuss the solution to it. The most popular way of achieving thread synchronization is by using Mutexes.

A Mutex is a lock that we set before using a shared resource and release after using it. When the lock is set, no other thread can access the locked region of code. So we see that even if thread 2 is scheduled while thread 1 was not done accessing the shared resource and the code is locked by thread 1 using mutexes then thread 2 cannot even access that region of code. So this ensures a synchronized access of shared resources in the code.

Internally it works as follows :

  • Suppose one thread has locked a region of code using mutex and is executing that piece of code.
  • Now if scheduler decides to do a context switch, then all the other threads which are ready to execute the same region are unblocked.
  • Only one of all the threads would make it to the execution but if this thread tries to execute the same region of code that is already locked then it will again go to sleep.
  • Context switch will take place again and again but no thread would be able to execute the locked region of code until the mutex lock over it is released.
  • Mutex lock will only be released by the thread who locked it.
  • So this ensures that once a thread has locked a piece of code then no other thread can execute the same region until it is unlocked by the thread who locked it.
  • Hence, this system ensures synchronization among the threads while working on shared resources.

A mutex is initialized and then a lock is achieved by calling the following two functions :

int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_lock(pthread_mutex_t *mutex);

The first function initializes a mutex and through second function any critical region in the code can be locked.

The mutex can be unlocked and destroyed by calling following functions :

int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);

The first function above releases the lock and the second function destroys the lock so that it cannot be used anywhere in future.

A Practical Example

Lets see a piece of code where mutexes are used for thread synchronization

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

pthread_t tid[2];
int counter;
pthread_mutex_t lock;

void* doSomeThing(void *arg)
{
    pthread_mutex_lock(&lock);

    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);

    for(i=0; i<(0xFFFFFFFF);i++);

    printf("\n Job %d finished\n", counter);

    pthread_mutex_unlock(&lock);

    return NULL;
}

int main(void)
{
    int i = 0;
    int err;

    if (pthread_mutex_init(&lock, NULL) != 0)
    {
        printf("\n mutex init failed\n");
        return 1;
    }

    while(i < 2)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    pthread_mutex_destroy(&lock);

    return 0;
}

In the code above :

  • A mutex is initialized in the beginning of the main function.
  • The same mutex is locked in the ‘doSomeThing()’ function while using the shared resource ‘counter’
  • At the end of the function ‘doSomeThing()’ the same mutex is unlocked.
  • At the end of the main function when both the threads are done, the mutex is destroyed.

Now if we look at the output, we find :

$ ./threads
Job 1 started
Job 1 finished
Job 2 started
Job 2 finished

So we see that this time the start and finish logs of both the jobs were present. So thread synchronization took place by the use of Mutex.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • chmurli May 3, 2012, 3:18 am

    This example is utterly bad! Using mutex this way you complely make threads useless!

  • Himanshu May 3, 2012, 4:23 am

    @chmurli
    Can you provide some details regarding your comment?

  • chmurli May 3, 2012, 2:01 pm

    Basically you had blocked second thread until first thread is comleted (by using mutex). This way all advantages of using threads are gone.

  • Himanshu May 4, 2012, 2:45 am

    @chmurli
    This articles speaks of thread synchronization using mutex and hence I have given a small example on how to use mutex. I have used mutex on a critical section of code and I dont think this is wrong.

  • engeland May 4, 2012, 3:57 am

    I have to agree with chmurli in every sense! Writing code for threads in this way is just a failure! A global variable is a bad example for restricted resources which have to be protected by mutexes. At least comment on your choice, so novices are not tempted to write such code in production code. A slightly (still not good) better example it would have been if you protect a copy of the global variable into a thread local one.

  • Himanshu Arora May 5, 2012, 2:17 am

    @engeland
    “A global variable is a bad example for restricted resources which have to be protected by mutexes”
    Can you please justify your statement?

  • gatsu May 16, 2012, 5:02 pm

    @engeland
    The article (specifically part 4) is not about writing multi threaded code. The author is simply showing how mutexes are used.
    The article is good and helpful because it shows failing and passing cases.

    Thanks,

  • engeland May 21, 2012, 4:14 am

    @gatsu: mutexes are about multi threaded code. Mutexes in single threaded code are nonsense.

  • fok May 25, 2012, 7:35 am

    @engeland: Why you thing that using global variable is wrong int this case? Just because it is bad programmer practice it doesn’t mean it is bad example of resource, that can be protected by mutexes. Should have Himanshu use static variables? He could but would be code more understandable to newbies then? NO. And this is the key point of examples. Make it as simple as possible. On the other hand you are right that it might have been mentioned that global variables are bad programming habit.
    @chmurli: Same as for global variable stands for your comment about single threaded code. This is total simplification of mutex synchronization. Whole code is part of the critical section and thus protected. In that case is it of course works serially. Just add anything before pthread_mutex_lock or after pthread_mutex_unlock and you have nice multithreaded program.
    @Himansu: Thanks for this nice example.

  • Himanshu May 25, 2012, 12:12 pm

    @fok
    Thanks a lot for pointing out precisely what I wanted to convey through this article.

  • Saurabh Arora July 3, 2012, 2:55 am

    Hi Himanshu,
    Whatever ppl say this link is very useful for newbies.
    Ppl should appreciate the efforts for explain threads in so simple form.
    If Ppl want more advance examples they can refer to google and get numerous tough examples which even they can’t solve.
    Any way I sincerely thank you for your efforts.

  • Himanshu July 5, 2012, 10:01 pm

    @Saurabh Arora
    Thanks Saurabh!!!!

  • Eugene August 23, 2012, 11:44 am

    This article explained me exactly what I needed to understand: what mutex is for.
    To those who criticise (chmurli,…): Please understand that people sometimes just need exactly what they are looking for. I wanted to understand mutex. I did not need explanations about the whole threads theory, parallel programming concepts, etc… And I am not a beginner in threads… (Used in Java, Windows…) I just never used POSIX threads before and this article helped me a lot. I want to thank the author.

  • Senthil October 17, 2012, 12:43 pm

    Your article is very helpful for newb. Thanks for the article.

  • Jack November 5, 2012, 10:16 am

    I was searching for an example on how (not) synchronization of threads could go wrong. My programming books only tell me to ‘watch out’, but it is far more fun to see it go wrong in my own console. The code you provided is exactly what I was searching for, so thanks!

  • harry December 12, 2012, 9:32 am

    thanks a lots for understanding of mutex

  • neevek January 10, 2013, 10:57 pm

    This article is really helpful, it explained when synchronization is needed, and how to use mutex to synchronize code, which I believe is what the article is meant to be conveyed. Thank you Himanshu, for your efforts.

  • kartheek January 28, 2013, 1:38 am

    Thanks for this example, i got what i want from this

  • Alex February 8, 2013, 9:57 am

    Great article for noobs like me 😛

  • pizza March 18, 2013, 5:35 am

    I think chmurli has made a valid point where the second thread is being blocked until thread one is finished. @Himansu can you please let me know how can we solve this?Thanks for the article.

  • James March 21, 2013, 6:15 pm

    This is horrible. Not only is chmurli correct, you can’t return NULL from a void function. NULL is an actual integer value. This function is void not int.

  • James March 21, 2013, 6:33 pm

    My bad … void*

  • Pras March 27, 2013, 9:25 am

    I am totally new to Threads, Mutexes, after reading this example, I got the Basic of MUTEX & its use case. With this knowledge, I think, I can understand any other Mutex applications.

    But, my humble request to author, is to provide additional Example, which could answer the critics (basically geeks).

  • badboy March 30, 2013, 2:46 pm

    Greeting folks,

    I think author could have done better. I see few problems in this article. Let me try to improve it. Before explaining mutexes or other synchronization mechanisms, let me try to explain why we need synchronization mechanisms.

    Suppose, a bank has a transaction processing system to manage online transactions of account holders and it has one process running(account holders data loaded in memory as global shared data) which, when any transaction request comes creates a thread to serve it. As we know that threads of a process have access to the global data and can execute concurrently. Simultaneous modification of shared data may result in data inconsistency. Let us see how….
    Suppose, you have a bank account with some balance amount Rs. 100. Its not enough to meet one month expenditure, so you asked your dad and brother to deposit some money in your account. Now, suppose your dad and brother both tried to deposit money at the same time. To serve their request, server process creates two threads, one for each to let them deposit money. Suppose, to deposit money, thread updates account balance as follows –
    1. OldBalance = your_account.balance
    2. NewBalance = OldBalance + depositedAmount
    3. your_account.balance = NewBalance
    Now in your dad and brother’s case they started depositing money at the same time so both threads take OldBalance = 100. Both threads do calculation of NewBalance separately as
    thread(1) NewBalance = 100 + 10000 ——> dad deposited Rs. 10000.
    thread(2) NewBalance = 100 + 5000 ——–> brother deposited Rs. 5000.
    Now suppose thread(2) updates your account balance as Rs. 5100, and just after that thread(1) updates your account balance to Rs. 10100. So, your final account balance is Rs. 10100. Happy???? Maybe not. So, in effect, the money deposited by your brother hasn’t been credited into your account. This is why we need synchronization mechanisms.

    Now, Lets implement one transaction processing system using mutex to fix data inconsistency problem.

    //: tp_system_using_mutex.c
    #include
    #include
    #include

    struct account {
    char name[20];
    int balance;
    };

    //Globally shared account details
    struct account yac = {“badboy”, 100};

    //Lock to be used while updating account balance
    pthread_mutex_t lock;

    void* deposit(void* data) {
    int ammount = *((int*)data);
    printf(“Account Holder: %s, Current balance: Rs. %d\n”, yac.name, yac.balance);

    printf(“Depositing Rs. %d…\n”, ammount);
    pthread_mutex_lock(&lock);
    yac.balance += ammount;
    pthread_mutex_unlock(&lock);

    printf(“Account Holder: %s, Updated balance: Rs. %d\n\n”, yac.name, yac.balance);

    return (void*)NULL;
    }

    int main(int argc, char** argv) {
    int i, n;
    n = 2; //Increase value of n to simulate more concurrent requests
    pthread_t tid[n];
    int deposit_ammount[n];

    //Fill the deposit_ammount array with random values between 1 to 10000
    for(i = 0; i < n; i++)
    deposit_ammount[i] = 1 + (int)(10000.0 * (rand()/(RAND_MAX + 1.0)));

    //n number of concurrent request to deposit money in account
    for(i = 0; i < n; i++)
    while(pthread_create(&tid[i], NULL, &deposit, (void*)&deposit_ammount[i])) {
    printf("Thread creation failed! Please try later!\n");
    sleep(1);
    }

    for(i = 0; i < n; i++)
    pthread_join(tid[i], NULL);

    return 0;
    }

    Save and compile as follows –
    $ gcc -lpthread

    If you see the output you may be surprised that both threads take same old account balance(Rs 100) but still the update value is correct, all hail to mutex :).

    …Namaste & cheers…

  • Bryson April 9, 2013, 3:16 pm

    For everyone saying this is a terrible example, you aren’t seeing what this was really suppose to be. He didn’t say anywhere this is suppose to be multi-threaded. It helped me solve the single lane bridge problem. Try doing that problem with multiple threads (or multi-threaded) with NO critical sections and just try and tell me you didn’t have any threads get deadlocked. The only difference between what I had to do and this guy is one additional array of threads, another function, and instead of a counter, I used char*.

  • Amit May 28, 2013, 12:34 am

    Thanks

  • ShreeRaam July 11, 2013, 12:13 am

    Really good article on threads and mutexes.
    It’s a very useful in understanding the thread concepts for the beginners .
    Appreciate your effort.

    Thanks,
    ShreeRaam

  • Rajat September 4, 2013, 9:43 pm

    I thought this was a really nice example !!! Thanks Himanshu and Bad Boy !!! I would like chmurli write a more practical but succinct example of how threads are used in the real world. My understanding is that Mutexes lock global resources, be it a counter or whatever and a thread needs to lock the resource as specified in the example in order to use it….so not sure why some think this is a bad example of how to implement Mutexes. As a matter-of-fact this is exactly how Threads implement Mutexes…. A follow on example could deal with the hazards of Mutexes..deadlocks and such…the O’Riley book on Pthread does an excellent job….

  • Sheshu October 10, 2013, 9:11 pm

    Nice Article. All of them. Thanks for the Examples, Very Simple and Understandable :). Keep Posting

  • Khaled Ziyaeen November 4, 2013, 8:12 pm

    Good Article! This isn’t about threads so much so as it is about using mutex locks in C. Fantastic! Thank you!

  • Ramesh January 6, 2014, 12:42 pm

    Hi,
    This article is very good, i searched in so many sites & books but i unable to understand properly. but this article helped me a lot. Thank you so much author.

    @badboy: your explanation about mutex is so good. Thanks a lot.

  • Gabriel Dias January 29, 2014, 5:12 am

    Himanshu and BadBoy.

    Thank you for the helpful explanations.

  • Benyas February 8, 2014, 7:37 am

    Je vous remercie pour ces explications.

    Grossomodo les mutex servent à gérer les threads pour qu’il n’y a pas ce qu’on appelle interférence entre deux threads ou chevauchement.

    La mutex protège l’exécution d’un premier thread jusqu’à ce que l’exécution finisse, puis la mutex se dévérouille pour commencer un nouveau processus.

    —-French to English translation—-
    Thank you for these explanations.

    Grossomodo mutexes are used to manage threads so that there is no so-called interference or overlap between two threads.

    The mutex protects the execution of a first thread until execution finishes, then unlocks the mutex to start a new process.

  • Denis March 26, 2014, 2:24 am

    Thanks for this article! I just read a script of my lecture and did actually not understand anything about Mutexes, especially NOT how to use them.
    Your example is great for understanding the basics and a.t.m. it doesn’t matter to me how it could be done better. 😀

    Greetings

  • Pramod April 24, 2014, 10:05 pm

    Hi,

    Thanks for the example on using mutex locks.

    The above example works well ‘counter’ variable declared as static. when i executed the example using gcc.

  • Jeff Su October 1, 2014, 5:16 am

    Hello everyone,
    I try to run the code in qemu, and add more thread to join. for example, I add 5 threads and join them.
    But it doesn’t work, is there any one could help me…
    jeff@jeff-VirtualBox:~/arm-lecture-my/arm-lecture/lab-2$ make clean; make qemu
    rm -f atomic.o mutex.o atomic
    arm-linux-gnueabihf-gcc -c -pthread -static -o atomic.o atomic.c
    arm-linux-gnueabihf-gcc -c -pthread -static -o mutex.o mutex.s
    arm-linux-gnueabihf-gcc -pthread -static -o atomic atomic.o mutex.o
    qemu-arm ./atomic
    create thread 0 ok

    Job 1 started
    create thread 1 ok
    create thread 2 ok
    qemu: uncaught target signal 11 (Segmentation fault) – core dumped
    make: *** [qemu] Segmentation fault (core dumped)

    here’s my Makefile:
    CC = arm-linux-gnueabihf-gcc
    CFLAGS = -pthread -static

    objects = atomic.o mutex.o

    default: atomic

    .PHONY: default clean

    atomic: $(objects)
    $(CC) $(CFLAGS) -o $@ $^

    atomic.o: atomic.c
    mutex.o: mutex.s

    %.o: %.c
    $(CC) -c $(CFLAGS) -o $@ $<

    %.o: %.s
    $(CC) -c $(CFLAGS) -o $@ $<

    clean:
    rm -f $(objects) atomic

    qemu: atomic
    qemu-arm ./atomic
    # qemu-arm -g 1234 -L /usr/arm-linux-gnueabihf ./atomic &
    # arm-none-eabi-gdb ./atomic

  • leelanaresh March 1, 2015, 3:05 am

    thank you nice explanation

  • Charan May 25, 2015, 10:49 pm

    Good job by the writer!. I understood all about what this article wanted to convey. Chmurli you should give an example and explain as badboy did. Thank you bad boy for giving a nice example.

  • MetthewsEmbeddedSystems June 13, 2015, 11:50 am

    I disagree, this was a wonderfully clear example for understanding how a MUTEX works and opperates. Offcourse the example undermines the use of it’s own threads, but that’s not what this example is about. Mind this article is meant for beginners in this field. And obvious this is a very basic example which explains very clear the working the MUTEX, with adding almost no extra mental overhead concerning multithreading, which this article isn’t about. Thanks to the writer, this really helped a beginner like me, completely new to multithreading. I now have a very clear understanding on the working of MUTEX and how to implement this! Wish more tutorials were as comprehensive as this one! Excuse me for possible grammer mistakes.

  • mehran June 16, 2015, 3:39 pm

    that was useful thanks

  • Anonymous December 8, 2015, 1:52 am

    Good Explanation.

    Keep Posting .Thank you

  • hari January 21, 2017, 9:26 am

    Keep posting goodone

  • Gajanan Patil May 3, 2017, 10:21 am

    in pthread_mutex_init(); what is the use of second argument please explain…

  • coarist March 2, 2019, 9:22 am

    The example is a good demonstartion.
    Multi-threading is a complex subject.
    Using simple illustration helps understanding the mechanism.
    The code example given helps relate observation to actual code thus enhancing understanding.

    Just like “Hello World!” is a valid C program, the example code given is valid in the same way.