📄 pthread_cond_init.man
字号:
.TH PTHREAD_COND 3 LinuxThreads.XREF pthread_cond_signal.XREF pthread_cond_broadcast.XREF pthread_cond_wait.XREF pthread_cond_timedwait.XREF pthread_cond_destroy.SH NAMEpthread_cond_init, pthread_cond_destroy, pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait \- operations on conditions.SH SYNOPSIS#include <pthread.h>pthread_cond_t cond = PTHREAD_COND_INITIALIZER;int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);int pthread_cond_signal(pthread_cond_t *cond);int pthread_cond_broadcast(pthread_cond_t *cond);int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);int pthread_cond_destroy(pthread_cond_t *cond);.SH DESCRIPTIONA condition (short for ``condition variable'') is a synchronizationdevice that allows threads to suspend execution and relinquish theprocessors until some predicate on shared data is satisfied. The basicoperations on conditions are: signal the condition (when thepredicate becomes true), and wait for the condition, suspending thethread execution until another thread signals the condition.A condition variable must always be associated with a mutex, to avoidthe race condition where a thread prepares to wait on a conditionvariable and another thread signals the condition just before thefirst thread actually waits on it.!pthread_cond_init! initializes the condition variable |cond|, using thecondition attributes specified in |cond_attr|, or default attributesif |cond_attr| is !NULL!. The LinuxThreads implementation supports noattributes for conditions, hence the |cond_attr| parameter is actuallyignored.Variables of type !pthread_cond_t! can also be initializedstatically, using the constant !PTHREAD_COND_INITIALIZER!.!pthread_cond_signal! restarts one of the threads that are waiting onthe condition variable |cond|. If no threads are waiting on |cond|,nothing happens. If several threads are waiting on |cond|, exactly oneis restarted, but it is not specified which.!pthread_cond_broadcast! restarts all the threads that are waiting onthe condition variable |cond|. Nothing happens if no threads arewaiting on |cond|.!pthread_cond_wait! atomically unlocks the |mutex| (as per!pthread_unlock_mutex!) and waits for the condition variable |cond| tobe signaled. The thread execution is suspended and does not consumeany CPU time until the condition variable is signaled. The |mutex|must be locked by the calling thread on entrance to!pthread_cond_wait!. Before returning to the calling thread,!pthread_cond_wait! re-acquires |mutex| (as per !pthread_lock_mutex!).Unlocking the mutex and suspending on the condition variable is doneatomically. Thus, if all threads always acquire the mutex beforesignaling the condition, this guarantees that the condition cannot besignaled (and thus ignored) between the time a thread locks the mutexand the time it waits on the condition variable.!pthread_cond_timedwait! atomically unlocks |mutex| and waits on|cond|, as !pthread_cond_wait! does, but it also bounds the durationof the wait. If |cond| has not been signaled within the amount of timespecified by |abstime|, the mutex |mutex| is re-acquired and!pthread_cond_timedwait! returns the error !ETIMEDOUT!. The |abstime| parameter specifies an absolute time, with the sameorigin as !time!(2) and !gettimeofday!(2): an |abstime| of 0corresponds to 00:00:00 GMT, January 1, 1970.!pthread_cond_destroy! destroys a condition variable, freeing theresources it might hold. No threads must be waiting on the conditionvariable on entrance to !pthread_cond_destroy!. In the LinuxThreadsimplementation, no resources are associated with condition variables,thus !pthread_cond_destroy! actually does nothing except checking thatthe condition has no waiting threads..SH CANCELLATION!pthread_cond_wait! and !pthread_cond_timedwait! are cancellationpoints. If a thread is cancelled while suspended in one of thesefunctions, the thread immediately resumes execution, then locks againthe |mutex| argument to !pthread_cond_wait! and!pthread_cond_timedwait!, and finally executes the cancellation.Consequently, cleanup handlers are assured that |mutex| is locked whenthey are called..SH "ASYNC-SIGNAL SAFETY"The condition functions are not async-signal safe, and should not becalled from a signal handler. In particular, calling!pthread_cond_signal! or !pthread_cond_broadcast! from a signalhandler may deadlock the calling thread..SH "RETURN VALUE"All condition variable functions return 0 on success and a non-zeroerror code on error..SH ERRORS!pthread_cond_init!, !pthread_cond_signal!, !pthread_cond_broadcast!,and !pthread_cond_wait! never return an error code.The !pthread_cond_timedwait! function returns the following error codeson error:.RS.TP!ETIMEDOUT!the condition variable was not signaled until the timeout specified by|abstime|.TP!EINTR!!pthread_cond_timedwait! was interrupted by a signal.REThe !pthread_cond_destroy! function returns the following error codeon error:.RS.TP!EBUSY!some threads are currently waiting on |cond|..RE.SH AUTHORXavier Leroy <Xavier.Leroy@inria.fr>.SH "SEE ALSO"!pthread_condattr_init!(3),!pthread_mutex_lock!(3),!pthread_mutex_unlock!(3),!gettimeofday!(2),!nanosleep!(2)..SH EXAMPLEConsider two shared variables |x| and |y|, protected by the mutex |mut|,and a condition variable |cond| that is to be signaled whenever |x|becomes greater than |y|..RS.ft 3.nf.spint x,y;pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t cond = PTHREAD_COND_INITIALIZER;.ft.LP.RE.fiWaiting until |x| is greater than |y| is performed as follows:.RS.ft 3.nf.sppthread_mutex_lock(&mut);while (x <= y) { pthread_cond_wait(&cond, &mut);}/* operate on x and y */pthread_mutex_unlock(&mut);.ft.LP.RE.fiModifications on |x| and |y| that may cause |x| to become greater than|y| should signal the condition if needed:.RS.ft 3.nf.sppthread_mutex_lock(&mut);/* modify x and y */if (x > y) pthread_mutex_broadcast(&cond);pthread_mutex_unlock(&mut);.ft.LP.RE.fiIf it can be proved that at most one waiting thread needs to be wakenup (for instance, if there are only two threads communicating through|x| and |y|), !pthread_cond_signal! can be used as a slightly moreefficient alternative to !pthread_cond_broadcast!. In doubt, use!pthread_cond_broadcast!.To wait for |x| to becomes greater than |y| with a timeout of 5seconds, do:.RS.ft 3.nf.spstruct timeval now;struct timespec timeout;int retcode;pthread_mutex_lock(&mut);gettimeofday(&now);timeout.tv_sec = now.tv_sec + 5;timeout.tv_nsec = now.tv_usec * 1000;retcode = 0;while (x <= y && retcode != ETIMEDOUT) { retcode = pthread_cond_timedwait(&cond, &mut, &timeout);}if (retcode == ETIMEDOUT) { /* timeout occurred */} else { /* operate on x and y */}pthread_mutex_unlock(&mut);.ft.LP.RE.fi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -