1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
.\" $OpenBSD: pthread_cond_init.3,v 1.14 2025/07/03 18:01:38 tedu Exp $
.\"
.\" Copyright (c) 1997 Brian Cully <shmit@kublai.com>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the author nor the names of any co-contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD: pthread_cond_init.3,v 1.6 1999/08/28 00:03:03 peter Exp $
.\" $FreeBSD: pthread_cond_destroy.3,v 1.5 1999/08/28 00:03:03 peter Exp $
.\" $FreeBSD: pthread_cond_wait.3,v 1.6 1999/08/28 00:03:04 peter Exp $
.\" $FreeBSD: pthread_cond_timedwait.3,v 1.6 1999/08/28 00:03:04 peter Exp $
.\" $FreeBSD: pthread_cond_broadcast.3,v 1.5 1999/08/28 00:03:03 peter Exp $
.\" $FreeBSD: pthread_cond_signal.3,v 1.5 1999/08/28 00:03:04 peter Exp $
.\"
.Dd $Mdocdate: July 3 2025 $
.Dt PTHREAD_COND_INIT 3
.Os
.Sh NAME
.Nm pthread_cond_init ,
.Nm pthread_cond_destroy ,
.Nm pthread_cond_wait ,
.Nm pthread_cond_timedwait ,
.Nm pthread_cond_broadcast ,
.Nm pthread_cond_signal
.Nd block and unblock threads with condition variables
.Sh SYNOPSIS
.Lb libpthread
.In pthread.h
.Ft int
.Fo pthread_cond_init
.Fa "pthread_cond_t *cond"
.Fa "const pthread_condattr_t *attr"
.Fc
.Ft int
.Fo pthread_cond_destroy
.Fa "pthread_cond_t *cond"
.Fc
.Ft int
.Fo pthread_cond_wait
.Fa "pthread_cond_t *cond"
.Fa "pthread_mutex_t *mutex"
.Fc
.Ft int
.Fo pthread_cond_timedwait
.Fa "pthread_cond_t *cond"
.Fa "pthread_mutex_t *mutex"
.Fa "const struct timespec *abstime"
.Fc
.Ft int
.Fo pthread_cond_broadcast
.Fa "pthread_cond_t *cond"
.Fc
.Ft int
.Fo pthread_cond_signal
.Fa "pthread_cond_t *cond"
.Fc
.Sh DESCRIPTION
The
.Fn pthread_cond_init
function creates a new condition variable with attributes specified by
.Fa attr .
If
.Fa attr
is
.Dv NULL ,
the default attributes are used.
.Pp
The
.Fn pthread_cond_destroy
function frees the resources associated with the condition variable
.Fa cond .
.Pp
The
.Fn pthread_cond_wait
function atomically blocks the current thread by waiting on the condition
variable
.Fa cond ,
and unlocks the mutex specified by
.Fa mutex .
The waiting thread unblocks only after another thread calls
.Fn pthread_cond_broadcast
or
.Fn pthread_cond_signal
with the same condition variable.
The
.Fn pthread_cond_timedwait
function does the same, but returns after the system time reaches
.Fa abstime .
In all cases, both functions then reacquire the lock on
.Fa mutex
before returning.
.Pp
The
.Fn pthread_cond_broadcast
function unblocks all threads waiting for the condition variable
.Fa cond .
The
.Fn pthread_cond_signal
function unblocks at least one thread waiting for the condition variable
.Fa cond .
.Sh RETURN VALUES
These functions return zero for success and positive error numbers
for failure.
.Sh ERRORS
.Fn pthread_cond_init
fails if:
.Bl -tag -width Er
.It Bq Er EINVAL
The value specified by
.Fa attr
is invalid.
.It Bq Er ENOMEM
The process cannot allocate enough memory to create another condition
variable.
.It Bq Er EAGAIN
The system temporarily lacks the resources to create another condition
variable.
.El
.Pp
The other functions fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
The value specified by
.Fa cond ,
.Fa mutex ,
or
.Fa abstime
is invalid.
.El
.Pp
.Fn pthread_cond_destroy
additionally fails if:
.Bl -tag -width Er
.It Bq Er EBUSY
The variable
.Fa cond
is locked by another thread.
.El
.Pp
.Fn pthread_cond_timedwait
additionally fails if:
.Bl -tag -width Er
.It Bq Er ETIMEDOUT
The system time has reached or exceeded the time specified in
.Fa abstime .
.El
.Sh SEE ALSO
.Xr pthread_condattr_init 3 ,
.Xr pthread_mutex_init 3
.Sh STANDARDS
These functions conform to
.St -p1003.1-96 .
|