The difference between the semaphore and the condition variable

Source: Internet
Author: User

Notice the difference between the semaphore and the condition variable the semaphore content is visible: http://www.cnblogs.com/charlesblc/p/6142868.html semaphore, shared memory, and Message Queuing the system V IPC Three Musketeers focus oninter-process communication, while conditional variables, mutexes, mainly focus oninter-thread communication。 The following is a reference: http://blog.chinaunix.net/uid-27164517-id-3282242.html pthread_cond_wait refers tocondition Variable, Sum aMutual exclusion lock combined with。 Inget the lock first before calling Pthread_cond_wait。 pthread_cond_wait function is executed firstAutomatic ReleaseSpecifies the lock, and then waits for the condition variable to change. The specified mutex is automatically set before the function call returnsRe-lock。

int pthread_cond_signal (pthread_cond_t * cond);

Pthread_cond_signal sends a message through the conditional variable cond, and if multiple messages are waiting, it wakes only one .

Pthread_cond_broadcast can wake up all. Call pthread_cond_signal after you want torelease the mutex immediately, because the last step of pthread_cond_wait is to re-lock the specified mutex, and if the mutex is not released after Pthread_cond_signal, Pthread_cond_wait still blocks. Regardless of the wait mode, you must mate with a mutex to prevent multiple threads from simultaneously requesting a race condition (Race Condition) for pthread_cond_wait () (or pthread_cond_timedwait (), hereinafter). Mutex mutex must be a normal lock (PTHREAD_MUTEX_TIMED_NP) or an adaptive lock (PTHREAD_MUTEX_ADAPTIVE_NP)
The type of mutex is the following: PTHREAD_MUTEX_TIMED_NP, which is the default value, which is the normal lock. When a line is Cheng, the remaining thread that requests the lock forms a wait queue, and the lock is acquired by priority after unlocking.  This locking strategy guarantees the fairness of resource allocation. pthread_mutex_recursive_np, nested locks allow the same thread to be successfully acquired multiple times for the same lock, and unlocked by multiple unlock.  If it is a different thread request, re-competes when the lock line threads unlocked. PTHREAD_MUTEX_ERRORCHECK_NP, checks the wrong lock, returns EDEADLK if the same thread requests the same lock, otherwise the same as the PTHREAD_MUTEX_TIMED_NP type action.  This ensures that the simplest case of deadlocks does not occur when multiple locks are not allowed. PTHREAD_MUTEX_ADAPTIVE_NP, Adaptive Lock, the simplest type of lock, only waiting to be unlocked after re-competition.

The difference between the semaphore and the condition variable

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.