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.
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.Get the Linux Sysadmin Course Now!
If you enjoyed this article, you might also like..
|
|
|
|






My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 25 comments… read them below or add one }
This example is utterly bad! Using mutex this way you complely make threads useless!
@chmurli
Can you provide some details regarding your comment?
Basically you had blocked second thread until first thread is comleted (by using mutex). This way all advantages of using threads are gone.
@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.
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.
@engeland
“A global variable is a bad example for restricted resources which have to be protected by mutexes”
Can you please justify your statement?
@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,
@gatsu: mutexes are about multi threaded code. Mutexes in single threaded code are nonsense.
@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.
@fok
Thanks a lot for pointing out precisely what I wanted to convey through this article.
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.
@Saurabh Arora
Thanks Saurabh!!!!
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.
Your article is very helpful for newb. Thanks for the article.
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!
thanks a lots for understanding of mutex
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.
Thanks for this example, i got what i want from this
Great article for noobs like me
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.
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.
My bad … void*
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).
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…
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*.