Pthread_cond_wait () function (excerpt)

Source: Internet
Author: User

Tag: OS: Use strong amp SP time on Ad c

 

/*********** Pthread_cond_wait **********/
Pthread_mutex_lock (& qlock);/* Lock */
Pthread_cond_wait (& qready, & qlock);/* block --> unlock --> wait () return --> lock */
Pthread_mutex_unlock (& qlock);/* unlock */

/*************************************** **************/

When pthread_mutex_lock () is called, pthread_cond_wait automatically unlocks mutex after the thread is put into the blocking queue.

Other threads can obtain the lock right. In this way, other threads can access critical resources and wake up the blocked process as appropriate.

When pthread_cond_wait returns, it automatically locks mutex.

 

understanding the role of pthread_cond_wait () is very important-it is the core of the POSIX thread signal sending system and the most difficult to understand.
first, let's consider the following situation: the thread locks the mutex object to view the linked list, but the list happens to be empty. This particular thread cannot do anything-it is designed to remove nodes from the list, but now there are no nodes. Therefore, it can only:
when the mutex object is locked, the thread will call pthread_cond_wait (& mycond, & mymutex ). Pthread_cond_wait () calls are quite complex, so we only execute one operation at a time.
the first thing pthread_cond_wait () Does is to unlock the mutex object at the same time (so other threads can modify the linked list ), and wait for the condition mycond to occur (when pthread_cond_wait () receives the "signal" from another thread, it will wake up ). The mutex object has been unlocked. Other threads can access and modify the linked list, and may add items. [ An atomic operation requires unlocking and blocking ]

At this time, the call to pthread_cond_wait () has not yet returned. Unlocking A mutex will happen immediately, but the waiting condition mycond is usually a blocking operation, which means the thread will sleep and will not consume the CPU cycle until it wakes up. This is exactly what we are looking forward. ThreadSleep until specific conditions occurDuring this period, no busy queries that waste CPU time will occur. From the thread point of view, it is only waiting for the pthread_cond_wait () call to return.

Now, let's continue to explain that another thread (called thread 2) locks mymutex and adds an item to the linked list. After the mutex object is unlocked, thread 2 immediately calls the pthread_cond_broadcast (& mycond) function ). After this operation, thread 2 will immediately wake up all threads waiting for the mycond condition variable. This means that the first thread (still in the pthread_cond_wait () call) willWake up.

Now, let's take a look at what happened to the first thread. You may think that after thread 2 calls pthread_cond_broadcast (& mymutex), The pthread_cond_wait () of thread 1 will return immediately. That's not the case! In fact, pthread_cond_wait () will execute the last operation:Re-lock mymutex. Once pthread_cond_wait () locks the mutex object, it will return and allow thread 1 to continue execution. At that time, it can immediately check the list and view the changes it is interested in.

 

Pthread_cond_wait () function (excerpt)

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.