A detailed explanation of the mutex and condition variables under Linux

Source: Internet
Author: User
Tags mutex thread

1. The first definition of pthread_cond_wait is this

The pthread_cond_wait () and pthread_cond_timedwait () functions are used to blocks on a condition variable. They are called with a mutex locked by the calling thread or undefined behaviour would result.

These functions atomically release mutexes and cause the calling thread to blocks on the condition variable h ere means "atomically with respect to access by Anotherthread to the mutex and then the condition". This is, Ifanother thread are able to acquire the mutex after the About-to-blockthread has released it, then a subsequent C All topthread_cond_signal () or pthread_cond_broadcast () in this thread behaves as if it were issued after the About-to-blo CK thread has blocked.

2. As can be seen from the above interpretation, pthread_cond_wait () must be used in support of Pthread_mutex. (The internal operation of a wait: a unclock in the wait state, lock before the end of the wait)

The pthread_cond_wait () function automatically release the mutex as soon as it enters the wait state.

In Thread1:

Pthread_mutex_lock (&m_mutex);

Pthread_cond_wait (&m_cond,&m_mutex);

Pthread_mutex_unlock (&m_mutex);

In Thread2:

Pthread_mutex_lock (&m_mutex);

Pthread_cond_signal (&m_cond);

Pthread_mutex_unlock (&m_mutex);

Why do you use it with Pthread_mutex? This is to respond to thread 1 when the call to Pthread_cond_wait () but thread 1 has not entered the state of wait cond, when thread 2 invokes the Cond_singal case. This cond_singal is lost without a mutex lock. In the case of a lock, thread 2 must wait until the mutex is freed (that is, pthread_cod_wait () enters the Wait_cond state and automatically frees the mutex) to invoke Cond_singal (if thread 2 also uses a mutex).

3. Pthread_cond_wait () automatically lock the mutex when the cond condition is successfully obtained by wait.

There is another problem. This is because

The pthread_cond_wait () and pthread_cond_timedwait () is a cancellation point.

In Thread3:

Pthread_cancel (&m_thread);

Pthread_join ();

Because Pthread_cond_wait () and pthread_cond_timedwait () is the thread exit point function, so in Thread3

You can call Pthread_cancel () to exit thread 1.    So obviously thread 1 will be in pthread_cond_wait (&m_cond,&m_mutex); and Pthread_mutex_unlock (&m_mutex); Exit between, the Pthread_cond_wait () function automatically lock the mutex, this time the thread 1 exit (and did not run to Pthread_mutex_unlock ()), if Thread2 this time will never get lock state.

The usual solution to this problem is as follows

void Cleanup (void *arg)

{

Pthread_mutex_unlock (&mutex);

}

void* thread1 (void* Arg)

{

Pthread_cleanup_push (cleanup, NULL); Thread Cleanup Handler

Pthread_mutex_lock (&mutex);

Pthread_cond_wait (&cond, &mutex);

Pthread_mutex_unlock (&mutex);

Pthread_cleanup_pop (0);

}

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.