(For Solaris threads, see "cond_signal(3THR)".) Prototype: ![]() Use pthread_cond_signal(3THR) to unblock one thread that is blocked on the condition variable pointed to by cv. Unblock a Specific Thread pthread_cond_signal(3THR) The value specified by cv or mp is invalid. When the following condition occurs, the function fails and returns the corresponding value. Any other returned value indicates that an error occurred. Pthread_cond_wait() returns zero after completing successfully. If a cancel is pending and the calling thread has cancellation enabled, the thread terminates and begins executing its cleanup handlers while continuing to hold the lock. Pthread_cond_wait() is a cancellation point. ![]() No specific order of acquisition is guaranteed when more than one thread blocks on the condition variable. The recommended test method is to write the condition check as a while() loop that calls pthread_cond_wait(). This causes one or all of the threads waiting on the condition to unblock and to try to acquire the mutex lock again.īecause the condition can change before an awakened thread returns from pthread_cond_wait(), the condition that caused the wait must be retested before the mutex lock is acquired. The condition variable is then signaled by another thread when it changes the condition value. When the condition expression is false, the thread blocks on the condition variable. In typical use, a condition expression is evaluated under the protection of a mutex lock. It atomically releases the associated mutex lock before blocking, and atomically acquires it again before returning. This function blocks until the condition is signaled. The pthread_cond_wait() routine always returns with the mutex locked and owned by the calling thread, even when returning an error. The blocked thread can be awakened by a pthread_cond_signal(), a pthread_cond_broadcast(), or when interrupted by delivery of a signal.Īny change in the value of a condition associated with the condition variable cannot be inferred by the return of pthread_cond_wait(), and any such condition must be reevaluated. Int pthread_cond_wait(pthread_cond_t * cv,pthread_mutex_t * mutex) #include (For Solaris threads, see "cond_wait(3THR)".) Prototype: Use pthread_cond_wait(3THR) to atomically release the mutex pointed to by mp and to cause the calling thread to block on the condition variable pointed to by cv. ![]() Block on a Condition Variable pthread_cond_wait(3THR) There is not enough memory to initialize the condition variable. The necessary resources are not available. When any of the following conditions occurs, the function fails and returns the corresponding value. Pthread_cond_init() returns zero after completing successfully. If a condition variable is reinitialized or destroyed, the application must be sure the condition variable is not in use. Multiple threads must not simultaneously initialize or reinitialize the same condition variable. This has the same effect as dynamically allocating pthread_cond_init() with null attributes. Statically defined condition variables can be initialized directly to have default attributes with the macro PTHREAD_COND_INITIALIZER. * initialize a condition variable to its default value */ Int pthread_cond_init(pthread_cond_t * cv,Ĭonst pthread_condattr_t * cattr) #include (For Solaris threads, see "cond_init(3THR)".) Prototype: The effect of cattr being NULL is the same as passing the address of a default condition variable attribute object, but without the memory overhead. Use pthread_cond_init(3THR) to initialize the condition variable pointed at by cv to its default value ( cattr is NULL), or to specify condition variable attributes that are already set with pthread_condattr_init(). ![]() Initialize a Condition Variable pthread_cond_init(3THR) Table 4-6 lists the functions that are available. This section explains using condition variables.
0 Comments
Leave a Reply. |