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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
|
.\" Copyright (C) 1994-1999 Free Software Foundation, Inc.
.\"
.\" Permission is granted to make and distribute verbatim copies of
.\" this manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of
.\" this manual under the conditions for verbatim copying, provided that
.\" the entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Permission is granted to copy and distribute translations of this
.\" manual into another language, under the above conditions for modified
.\" versions, except that this permission notice may be stated in a
.\" translation approved by the Foundation.
.\"
.\" Copyright (C) 1999 Xavier Leroy.
.\"
.\" Japanese Version Copyright (C) 2000 WAKABAYASHI, Takeyasu
.\" all rights reserved.
.\" Translated on Fri Jan 14 16:50:24 JST 2000
.\" by WAKABAYASHI, Takeyasu <twakaba@eco.toyama-u.ac.jp>
.\"
.\"WORD: predicate Ҹ
.\"WORD: atomically ©
.\" .TH PTHREAD_COND 3 LinuxThreads
.\" .SH NAME
.\" pthread_cond_init, pthread_cond_destroy, pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait \- operations on conditions
.\" .SH SYNOPSIS
.\" .B #include <pthread.h>
.\" .BI "pthread_cond_t " cond " = PTHREAD_COND_INITIALIZER;"
.\" .BI "int pthread_cond_init(pthread_cond_t *" cond ", pthread_condattr_t *" cond_attr ");"
.\" .BI "int pthread_cond_signal(pthread_cond_t *" cond ");"
.\" .BI "int pthread_cond_broadcast(pthread_cond_t *" cond ");"
.\" .BI "int pthread_cond_wait(pthread_cond_t *" cond ", pthread_mutex_t *" mutex ");"
.\" .BI "int pthread_cond_timedwait(pthread_cond_t *" cond ", pthread_mutex_t *" mutex ", const struct timespec *" abstime ");"
.\" .BI "int pthread_cond_destroy(pthread_cond_t *" cond ");"
.TH PTHREAD_COND 3 LinuxThreads
.SH NAME
pthread_cond_init, pthread_cond_destroy, pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait \- ѿ
.SH
.B #include <pthread.h>
.BI "pthread_cond_t " cond " = PTHREAD_COND_INITIALIZER;"
.BI "int pthread_cond_init(pthread_cond_t *" cond ", pthread_condattr_t *" cond_attr ");"
.BI "int pthread_cond_signal(pthread_cond_t *" cond ");"
.BI "int pthread_cond_broadcast(pthread_cond_t *" cond ");"
.BI "int pthread_cond_wait(pthread_cond_t *" cond ", pthread_mutex_t *" mutex ");"
.BI "int pthread_cond_timedwait(pthread_cond_t *" cond ", pthread_mutex_t *" mutex ", const struct timespec *" abstime ");"
.BI "int pthread_cond_destroy(pthread_cond_t *" cond ");"
.\" .SH DESCRIPTION
.\" A condition (short for ``condition variable'') is a synchronization
.\" device that allows threads to suspend execution and relinquish the
.\" processors until some predicate on shared data is satisfied. The basic
.\" operations on conditions are: signal the condition (when the
.\" predicate becomes true), and wait for the condition, suspending the
.\" thread execution until another thread signals the condition.
.SH
(־ѿפξά) ϡͭǡФ뤢Ҹ줬
ޤǡåɤ¹ԤߤץåȤǽˤƱ
֤Ǥ롣ФŪϡ(Ҹ줬ˤʤä)
뤳Ȥȡ¾Υåɤޤǥåɤμ¹
ߤƾԤĤȤǤ롣
.\" A condition variable must always be associated with a mutex, to avoid
.\" the race condition where a thread prepares to wait on a condition
.\" variable and another thread signals the condition just before the
.\" first thread actually waits on it.
ѿϤĤǤ mutex ȷӤĤƤʤФʤʤϡ
륹åɤѿԤȤȤƤˡ¾Υåɤ
Υåɤºݤ˾ѿФԵ뤽ľ˾롢
Ȥ뤿Ǥ롣
.\" .B "pthread_cond_init"
.\" initializes the condition variable
.\" .IR "cond" ,
.\" using the
.\" condition attributes specified in
.\" .IR "cond_attr" ,
.\" or default attributes
.\" if
.\" .I "cond_attr"
.\" is
.\" .BR "NULL" .
.\" The LinuxThreads implementation supports no
.\" attributes for conditions, hence the
.\" .I "cond_attr"
.\" parameter is actually
.\" ignored.
.B "pthread_cond_init"
ϡѿ
.IR "cond"
.IR "cond_attr"
ǻꤵ줿°ޤ
.I "cond_attr"
.BR "NULL"
ǤСǥեȤ°ǽ롣
LinuxThreads μϡʤѿ°ˤбƤʤ
ơ
.I "cond_attr"
ѥϡ¤ΤȤ̵뤵롣
.\" Variables of type
.\" .B "pthread_cond_t"
.\" can also be initialized
.\" statically, using the constant
.\" .BR "PTHREAD_COND_INITIALIZER" .
.B "pthread_cond_t"
ѿϡ
.BR "PTHREAD_COND_INITIALIZER"
ȤäŪ˽뤳ȤǤ롣
.\" .B "pthread_cond_signal"
.\" restarts one of the threads that are waiting on
.\" the condition variable
.\" .IR "cond" .
.\" If no threads are waiting on
.\" .IR "cond" ,
.\" nothing happens. If several threads are waiting on
.\" .IR "cond" ,
.\" exactly one
.\" is restarted, but it is not specified which.
.B "pthread_cond_signal"
ϡѿ
.IR "cond"
ԵƤ륹åɤΰĤμ¹ԤƳ롣
.IR "cond"
ԤäƤ륹åɤʤСⵯʤ
ʣΥåɤ
.IR "cond"
ԤäƤСĤΤΤƳ뤬ɤǤ뤫
狼ʤ
.\" .B "pthread_cond_broadcast"
.\" restarts all the threads that are waiting on
.\" the condition variable
.\" .IR "cond" .
.\" Nothing happens if no threads are
.\" waiting on
.\" .IR "cond" .
.B "pthread_cond_broadcast"
.IR "cond"
ԵƤƤΥåɤμ¹ԤƳ롣
.IR "cond"
ԤäƤ륹åɤʤСⵯʤ
.\" .B "pthread_cond_wait"
.\" atomically unlocks the
.\" .I "mutex"
.\" (as per
.\" .BR "pthread_unlock_mutex" )
.\" and waits for the condition variable
.\" .I "cond"
.\" to
.\" be signaled. The thread execution is suspended and does not consume
.\" any CPU time until the condition variable is signaled. The
.\" .I "mutex"
.\" must be locked by the calling thread on entrance to
.\" .BR "pthread_cond_wait" .
.\" Before returning to the calling thread,
.\" .B "pthread_cond_wait"
.\" re-acquires
.\" .I "mutex"
.\" (as per
.\" .BR "pthread_lock_mutex" ).
.B "pthread_cond_wait"
(
.BR "pthread_mutex_unlock"
ˤ)
.I "mutex"
ΥåȾѿ
.I "cond"
ФԵ©ǹԤѿޤ
åɤμ¹Ԥߤ졢CPU ֤뤳ȤϤʤ
.I "mutex"
ϡ
.BR "pthread_cond_wait"
γϻǡƤӽФåɤˤäƥåƤʤ
ʤʤ
ƤӽФ¦Υåɤ
.B "pthread_cond_wait"
.I "mutex"
(
.BR "pthread_mutex_lock"
ˤä)Ƥӳ롣
.\" Unlocking the mutex and suspending on the condition variable is done
.\" atomically. Thus, if all threads always acquire the mutex before
.\" signaling the condition, this guarantees that the condition cannot be
.\" signaled (and thus ignored) between the time a thread locks the mutex
.\" and the time it waits on the condition variable.
mutex ΥåȾѿФԵϰ©˹Ԥ롣äơ
ƤΥåɤ˾ mutex ΤʤС
åɤ mutex åȡ줬ѿԤĻ
Ȥ֤λǡԤʤ(ä̵뤵)Ȥ
ԲǽȤʤ뤳Ȥݾڤ롣
.\" .B "pthread_cond_timedwait"
.\" atomically unlocks
.\" .I "mutex"
.\" and waits on
.\" .IR "cond" ,
.\" as
.\" .B "pthread_cond_wait"
.\" does, but it also bounds the duration
.\" of the wait. If
.\" .I "cond"
.\" has not been signaled within the amount of time
.\" specified by
.\" .IR "abstime" ,
.\" the mutex
.\" .I "mutex"
.\" is re-acquired and
.\" .B "pthread_cond_timedwait"
.\" returns the error
.\" .BR "ETIMEDOUT" .
.\" The
.\" .I "abstime"
.\" parameter specifies an absolute time, with the same
.\" origin as
.\" .BR "time" (2)
.\" and
.\" .BR "gettimeofday" (2):
.\" an
.\" .I "abstime"
.\" of 0
.\" corresponds to 00:00:00 GMT, January 1, 1970.
.B "pthread_cond_timedwait"
.B "pthread_cond_wait"
Ʊ©
.I "mutex"
Υå
.IR "cond"
ؤԵԤޤԤ֤ĹԤ
.I "cond"
.IR "abstime"
ǻꤵ줿ʤäΤʤС
mutex
.I "mutex"
Ƴ
.B "pthread_cond_timedwait"
ϡ顼
.BR "ETIMEDOUT"
֤
.I "abstime"
ѥ
.BR "time" (2)
.BR "gettimeofday" (2)
εƱл֤ꤹ롣ʤ
0
.I "abstime"
00:00:00 GMT, January 1, 1970 롣
.\" .B "pthread_cond_destroy"
.\" destroys a condition variable, freeing the
.\" resources it might hold. No threads must be waiting on the condition
.\" variable on entrance to
.\" .BR "pthread_cond_destroy" .
.\" In the LinuxThreads
.\" implementation, no resources are associated with condition variables,
.\" thus
.\" .B "pthread_cond_destroy"
.\" actually does nothing except checking that
.\" the condition has no waiting threads.
.B "pthread_cond_destroy"
Ͼѿ˲줬ݻƤǽΤ롣
.BR "pthread_cond_destroy"
γϻǡʤ륹åɤ⤽ξѿԤäƤƤϤʤ
LinuxThreads μǤϡʤѿտ路Ƥʤ
äơ
.B "pthread_cond_destroy"
ϡ郎ԵåɤäƤʤȤΤʳ
⤷ʤ
.\" .SH CANCELLATION
.\" .B "pthread_cond_wait"
.\" and
.\" .B "pthread_cond_timedwait"
.\" are cancellation
.\" points. If a thread is cancelled while suspended in one of these
.\" functions, the thread immediately resumes execution, then locks again
.\" the
.\" .I "mutex"
.\" argument to
.\" .B "pthread_cond_wait"
.\" and
.\" .BR "pthread_cond_timedwait" ,
.\" and finally executes the cancellation.
.\" Consequently, cleanup handlers are assured that
.\" .I "mutex"
.\" is locked when
.\" they are called.
.SH ä
.B "pthread_cond_wait"
.B "pthread_cond_timedwait"
ϡäݥȤǤ롣Τ줫δؿߤƤ
åɤäȡåɤľ˼¹ԤƳ
.B "pthread_cond_wait"
.BR "pthread_cond_timedwait"
.I "mutex"
ƥåǸ˼ä¹Ԥ롣
̤Ȥơåץϥɥ餬ƤӽФݤ
.I "mutex"
åƤ뤳Ȥݾڤ롣
.\" .SH "ASYNC-SIGNAL SAFETY"
.\" The condition functions are not async-signal safe, and should not be
.\" called from a signal handler. In particular, calling
.\" .B "pthread_cond_signal"
.\" or
.\" .B "pthread_cond_broadcast"
.\" from a signal
.\" handler may deadlock the calling thread.
.SH ƱʥФ
ؿƱʥФưǤϤʤäơ
ʥϥɥ餫ƤӽФ٤ǤϤʤäˡ
.B "pthread_cond_signal"
ޤ
.B "pthread_cond_broadcast"
Υʥϥɥ餫θƤӽФϡƤӽФåɤ
ǥåɥåǽ롣
.\" .SH "RETURN VALUE"
.\" All condition variable functions return 0 on success and a non-zero
.\" error code on error.
.SH ֤
Ƥξѿؿϡ 0 ֤顼ʤ
顼ɤ֤
.\" .SH ERRORS
.\" .BR "pthread_cond_init" ,
.\" .BR "pthread_cond_signal" ,
.\" .BR "pthread_cond_broadcast" ,
.\" and
.\" .B "pthread_cond_wait"
.\" never return an error code.
.SH 顼
.BR "pthread_cond_init" ,
.BR "pthread_cond_signal" ,
.BR "pthread_cond_broadcast" ,
.B "pthread_cond_wait"
ϡ褷ƥ顼ɤ֤ʤ
.\" The
.\" .B "pthread_cond_timedwait"
.\" function returns the following error codes
.\" on error:
.\" .RS
.\" .TP
.\" .B "ETIMEDOUT"
.\" the condition variable was not signaled until the timeout specified by
.\" .I "abstime"
.B "pthread_cond_timedwait"
ϡ顼˺ݤƼΥ顼ɤ֤:
.RS
.TP
.B "ETIMEDOUT"
ѿ
.I "abstime"
ǻꤵ줿¤ޤǤʤä
.\" .TP
.\" .B "EINTR"
.\" .B "pthread_cond_timedwait"
.\" was interrupted by a signal
.\" .RE
.TP
.B "EINTR"
.B "pthread_cond_timedwait"
ʥˤäƳޤ줿
.RE
.\" The
.\" .B "pthread_cond_destroy"
.\" function returns the following error code
.\" on error:
.\" .RS
.\" .TP
.\" .B "EBUSY"
.\" some threads are currently waiting on
.\" .IR "cond" .
.\" .RE
.B "pthread_cond_destroy"
ؿϡ顼˺ݤƼΥ顼ɤ֤:
.RS
.TP
.B "EBUSY"
줫Υåɤ
.IR "cond"
ФԵƤ롣
.RE
.\" .SH AUTHOR
.\" Xavier Leroy <Xavier.Leroy@inria.fr>
.\" .SH "SEE ALSO"
.\" .BR "pthread_condattr_init" (3),
.\" .BR "pthread_mutex_lock" (3),
.\" .BR "pthread_mutex_unlock" (3),
.\" .BR "gettimeofday" (2),
.\" .BR "nanosleep" (2).
.SH
Xavier Leroy <Xavier.Leroy@inria.fr>
.SH Ϣ
.BR "pthread_condattr_init" (3),
.BR "pthread_mutex_lock" (3),
.BR "pthread_mutex_unlock" (3),
.BR "gettimeofday" (2),
.BR "nanosleep" (2).
.\" .SH EXAMPLE
.\" Consider two shared variables
.\" .I "x"
.\" and
.\" .IR "y" ,
.\" protected by the mutex
.\" .IR "mut" ,
.\" and a condition variable
.\" .I "cond"
.\" that is to be signaled whenever
.\" .I "x"
.\" becomes greater than
.\" .IR "y" .
.SH
Ĥζͭѿ
.I "x"
.IR "y"
äơmutex
.IR "mut"
ˤݸƤȤ褦ˡѿ
.I "cond"
äơ
.I "x"
.IR "y"
礭ʤСȤ褦
.RS
.ft 3
.nf
.sp
int x,y;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
.ft
.LP
.RE
.fi
.\" Waiting until
.\" .I "x"
.\" is greater than
.\" .I "y"
.\" is performed as follows:
.I "x"
.I "y"
礭ʤޤԤĤˤϡ
Τ褦ˤɤ:
.RS
.ft 3
.nf
.sp
pthread_mutex_lock(&mut);
while (x <= y) {
pthread_cond_wait(&cond, &mut);
}
/* x y */
pthread_mutex_unlock(&mut);
.ft
.LP
.RE
.fi
.\" Modifications on
.\" .I "x"
.\" and
.\" .I "y"
.\" that may cause
.\" .I "x"
.\" to become greater than
.\" .I "y"
.\" should signal the condition if needed:
.I "x"
.I "y"
礭褦
.I "x"
.I "y"
ɬפ˱ơͤФʤʤ:
.RS
.ft 3
.nf
.sp
pthread_mutex_lock(&mut);
/* x y ѹ */
if (x > y) pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mut);
.ft
.LP
.RE
.fi
.\" If it can be proved that at most one waiting thread needs to be waken
.\" up (for instance, if there are only two threads communicating through
.\" .I "x"
.\" and
.\" .IR "y" ),
.\" .B "pthread_cond_signal"
.\" can be used as a slightly more
.\" efficient alternative to
.\" .BR "pthread_cond_broadcast" .
.\" In doubt, use
.\" .BR "pthread_cond_broadcast" .
ư٤åɤ°ĤǤ뤳Ȥμ¤ʤ
(㤨С
.I "x"
.IR "y"
̤Ƹή륹åɤĤʤΤʤ)
.B "pthread_cond_signal"
.BR "pthread_cond_broadcast"
ΡФΨŪʪȤƻѤǤ롣
Τˤ
.BR "pthread_cond_broadcast"
Ѥ衣
.\" To wait for
.\" .I "x"
.\" to becomes greater than
.\" .I "y"
.\" with a timeout of 5
.\" seconds, do:
.I "x"
.I "y"
礭ʤΤäλ¤ߤԤĤˤϼΤ褦ˤ:
.\" .RS
.\" .ft 3
.\" .nf
.\" .sp
.\" struct 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
.RS
.ft 3
.nf
.sp
struct 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) {
/* ॢ */
} else {
/* x y */
}
pthread_mutex_unlock(&mut);
.ft
.LP
.RE
.fi
|