Mutex and condition variable [Reproduced]

Source: Internet
Author: User

When does mutex need condition variable?
Assume that there is a shared resource sum, and the associated mutex is lock_s. assume that the sum operation of each thread is very simple and irrelevant to the sum State. For example, it is only sum ++. only mutex is enough. the programmer only needs to ensure that the lock is obtained before each thread operation, sum ++, and unlock.

The code for each thread will look like this
Add ()
{
Pthread_mutex_lock (lock_s );
Sum ++;
Pthread_mutex_unlock (lock_s );
}

If the operation is complex, assume that the operation of thread T0, T1, and T2 is sum ++, and thread T3 prints a message when sum reaches 100, and clears sum. in this case, if only mutex is used, T3 requires a loop. In each loop, lock_s is obtained first, and the sum state is checked. If sum> = 100, the data is printed and cleared, then unlock. if sum <100, then unlock and sleep () This thread is appropriate for a period of time.

At this time, the code for T0, T1, and T2 remains unchanged. The code for T3 is as follows:
Print ()
{
While (1)
{
Pthread_mutex_lock (lock_s );
If (sum & lt; 100)
{
Printf ("sum reach 100 !");
Pthread_mutex_unlock (lock_s );
}
Else
{
Pthread_mutex_unlock (lock_s );
MySQL _ thread_sleep (100 );
Return OK;
}
}
}

There are two problems with this method.
1) sum will not reach 100 in most cases, so for the T3 code, in most cases, it takes the else branch, only lock and unlock, and then sleep (). this wastes CPU processing time.
2) To save CPU processing time, T3 will sleep () for a period of time when the sum has not reached 100. this brings about another problem, that is, the T3 response speed decreases. t4 may wake up when sum reaches 200.
3) in this way, the programmer is in a dilemma when setting sleep () time. It is too short to save resources, and it is too long to reduce the response speed. This is really hard to do!

At this time, condition variable underpants fell from the sky, saving you.

You first define a condition variable.
Pthread_cond_t cond_sum_ready = pthread_cond_initializer;

Code T0, T1, and T2 only needs to be followed by two lines, like this
Add ()
{
Pthread_mutex_lock (lock_s );
Sum ++;
Pthread_mutex_unlock (lock_s );
If (sum & gt; = 100)
Pthread_cond_signal (& cond_sum_ready );
}
The T3 code is
Print
{
Pthread_mutex_lock (lock_s );
While (sum <100)
Pthread_cond_wait (& cond_sum_ready, & lock_s );
Printf ("sum is over 100 !");
Sum = 0;
Pthread_mutex_unlock (lock_s );
Return OK;
}

Note:
1) Before thread_cond_wait (), you must first lock the associated mutex, because if the target condition is not met, pthread_cond_wait () will actually unlock the mutex, and then block, lock the mutex again after the target condition is met, and then return.
2) Why is it while (sum <100) instead of IF (sum <100 )? This is because there is a time difference between pthread_cond_signal () and pthread_cond_wait (). In this case, there is another thread T4 that reduces sum to less than 100, after pthread_cond_wait () is returned, T3 should check the sum size again. this is the intention of using while.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.