pthread_cond_initのヘルプ・マニュアル
日本語 英語
pthread_cond_init --help
man pthread_cond_init
PTHREAD_COND(3) PTHREAD_COND(3)
NAME
pthread_cond_init, pthread_cond_destroy, pthread_cond_signal,
pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait - 条
件変数の操作
書式
#include
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);
説明
条件(「条件変数」の省略) は、共有データに対するある述語が満たされるまで
、スレッドが実行を停止しプロセッサを手放すことを可能にする同期装置で あ
る。条件に対する基本的な操作は、(述語が真になった場合に) 条件を送信する
ことと、他のスレッドが条件を送信するまでスレッドの実行を停止して条件 を
待つことである。
条件変数はいつでも mutex と結びつけられていなければならない。これは、あ
るスレッドが条件変数を待とうとしている時に、他のスレッドが、先のスレ ッ
ド が実際に条件変数に対して待機するその直前に条件を送信する、という競合
条件を避けるためである。
pthread_cond_init は、条件変数 cond を cond_attr で指定された条件属性、
ま たは cond_attr が NULL であれば、デフォルトの属性で初期化する。 Lin-
uxThreads の実装は、いかなる条件変数の属性にも対応していない。かくし て
、 cond_attr パラメタは、実のところ無視される。
型 pthread_cond_t の変数は、定数 PTHREAD_COND_INITIALIZER を使って静的
に初期化することもできる。
pthread_cond_signal は、条件変数 cond に備えて待機しているスレッドの 一
つ の実行を再開させる。 cond を待っているスレッドがなければ、何も起こら
ない。複数のスレッドが cond を待っていれば、ただ一つのものだけが再開 さ
れるが、どれであるかはわからない。
pthread_cond_broadcast は cond に備えて待機している全てのスレッドの実行
を再開させる。 cond を待っているスレッドがなければ、何も起こらない。
pthread_cond_wait は ( pthread_mutex_unlock による) mutex のアンロッ ク
と 条件変数 cond の送信に対する待機を一息で行う。条件変数が送信されるま
でスレッドの実行は停止され、CPU 時間を消費することはない。 mutex は 、
pthread_cond_wait の開始時点で、これを呼び出すスレッドによってロックさ
れていなければならな い 。 呼 び 出 し 側 の ス レ ッ ド に 戻 る 前 に
pthread_cond_wait は mutex を ( pthread_mutex_lock によって)再び獲得す
る。
mutex のアンロックと条件変数に対する待機は一息に行われる。従って、全 て
のスレッドが条件を送信する前に常に mutex を獲得するのならば、スレッドが
mutex をアンロックする時点と、それが条件変数を待つ時点との中間の時点 で
、 条件の送信が行なわれる(従って無視される)ことが不可能となることが保証
される。
pthread_cond_timedwait は pthread_cond_wait と同じく、一息で mutex のア
ン ロックと cond への待機を行う。しかしまた、待ち時間の長さの設定も行う
。 cond が abstime で指定された時間内に送信されなかったのならば、 mutex
mutex が再獲得され pthread_cond_timedwait は、エラー ETIMEDOUT を返す。
abstime パラメタは time(2) と gettimeofday(2) の起点を同じくする絶対 時
間を指定する。すなわち 0 の abstime は 00:00:00 GMT, January 1, 1970 に
相当する。
pthread_cond_destroy は条件変数を破壊し、それが保持している可能性のある
資 源を開放する。 pthread_cond_destroy の開始時点で、いかなるスレッドも
その条件変数を待っていてはいけない。 LinuxThreads の実装では、いかな る
資 源も条件変数に付随していない。従って、 pthread_cond_destroy は、条件
が待機スレッドを持っていないことを確かめる以外に何もしない。
取り消し
pthread_cond_wait および pthread_cond_timedwait は、取り消しポイント で
あ る。このいずれかの関数で停止しているスレッドが取り消されると、スレッ
ドは直ちに実行を再開し、 pthread_cond_wait と pthread_cond_timedwait の
mutex 引数を再ロックし、最後に取り消しを実行する。結果として、クリーン
アップハンドラが呼び出される際に mutex がロックされていることを保証され
る。
非同期シグナルに対する安全性
条 件関数は非同期シグナルに対して安全ではない。よって、シグナルハンドラ
から呼び出すべきで は な い 。 特 に 、 pthread_cond_signal ま た は
pthread_cond_broadcast のシグナルハンドラからの呼び出しは、呼び出しスレ
ッドをデッドロックする可能性がある。
返り値
全ての条件変数関数は、成功すると 0 を返し、エラーならば非ゼロのエラーコ
ードを返す。
エラー
pthread_cond_init, pthread_cond_signal, pthread_cond_broadcast, および
pthread_cond_wait は、決してエラーコードを返さない。
pthread_cond_timedwait は、エラーに際して次のエラーコードを返す:
ETIMEDOUT
条件変数が abstime で指定された時限までに送信されなかった
。
EINTR pthread_cond_timedwait がシグナルによって割り込まれた。
pthread_cond_destroy 関数は、エラーに際して次のエラーコードを返す:
EBUSY いずれかのスレッドが現在 cond に対して待機している。
著者
Xavier Leroy
関連項目
pthread_condattr_init(3), pthread_mutex_lock(3),
pthread_mutex_unlock(3), gettimeofday(2), nanosleep(2).
例
二つの共有変数 x と y があって、mutex mut により保護されているとしよ う
。 更に、条件変数 cond があって、 x が y より大きくなれば、送信されると
しよう。
int x,y;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
x が y より大きくなるまで待つには、次のようにすれば良い:
pthread_mutex_lock(&mut);
while (x <= y) {
pthread_cond_wait(&cond, &mut);
}
/* x と y の操作 */
pthread_mutex_unlock(&mut);
x を y よりも大きくするような x と y の操作は必要に応じて、条件を送信せ
ねばならない:
pthread_mutex_lock(&mut);
/* x と y を変更する */
if (x > y) pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mut);
起 動 すべきスレッドが最大限一つであることが確実ならば (例えば、 x と y
を通じて交流するスレッドが二つしかないのならば) 、 pthread_cond_signal
を pthread_cond_broadcast の、少しばかり効率的な代替物として使用できる
。疑問のある場合には pthread_cond_broadcast を使用せよ。
x が y より大きくなるのを五秒の時限を設けて待つには次のようにする:
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);
LinuxThreads PTHREAD_COND(3)
PTHREAD_COND_DESTROY(3P) POSIX Programmer’s Manual PTHREAD_COND_DESTROY(3P)
PROLOG
This manual page is part of the POSIX Programmer’s Manual. The Linux
implementation of this interface may differ (consult the corresponding
Linux manual page for details of Linux behavior), or the interface may
not be implemented on Linux.
NAME
pthread_cond_destroy, pthread_cond_init - destroy and initialize condi-
tion variables
SYNOPSIS
#include
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr);
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
DESCRIPTION
The pthread_cond_destroy() function shall destroy the given condition
variable specified by cond; the object becomes, in effect, uninitial-
ized. An implementation may cause pthread_cond_destroy() to set the
object referenced by cond to an invalid value. A destroyed condition
variable object can be reinitialized using pthread_cond_init(); the
results of otherwise referencing the object after it has been destroyed
are undefined.
It shall be safe to destroy an initialized condition variable upon
which no threads are currently blocked. Attempting to destroy a condi-
tion variable upon which other threads are currently blocked results in
undefined behavior.
The pthread_cond_init() function shall initialize the condition vari-
able referenced by cond with attributes referenced by attr. If attr is
NULL, the default condition variable attributes shall be used; the
effect is the same as passing the address of a default condition vari-
able attributes object. Upon successful initialization, the state of
the condition variable shall become initialized.
Only cond itself may be used for performing synchronization. The
result of referring to copies of cond in calls to pthread_cond_wait(),
pthread_cond_timedwait(), pthread_cond_signal(), pthread_cond_broad-
cast(), and pthread_cond_destroy() is undefined.
Attempting to initialize an already initialized condition variable
results in undefined behavior.
In cases where default condition variable attributes are appropriate,
the macro PTHREAD_COND_INITIALIZER can be used to initialize condition
variables that are statically allocated. The effect shall be equivalent
to dynamic initialization by a call to pthread_cond_init() with parame-
ter attr specified as NULL, except that no error checks are performed.
RETURN VALUE
If successful, the pthread_cond_destroy() and pthread_cond_init() func-
tions shall return zero; otherwise, an error number shall be returned
to indicate the error.
The [EBUSY] and [EINVAL] error checks, if implemented, shall act as if
they were performed immediately at the beginning of processing for the
function and caused an error return prior to modifying the state of the
condition variable specified by cond.
ERRORS
The pthread_cond_destroy() function may fail if:
EBUSY The implementation has detected an attempt to destroy the object
referenced by cond while it is referenced (for example, while
being used in a pthread_cond_wait() or pthread_cond_timedwait())
by another thread.
EINVAL The value specified by cond is invalid.
The pthread_cond_init() function shall fail if:
EAGAIN The system lacked the necessary resources (other than memory) to
initialize another condition variable.
ENOMEM Insufficient memory exists to initialize the condition variable.
The pthread_cond_init() function may fail if:
EBUSY The implementation has detected an attempt to reinitialize the
object referenced by cond, a previously initialized, but not yet
destroyed, condition variable.
EINVAL The value specified by attr is invalid.
These functions shall not return an error code of [EINTR].
The following sections are informative.
EXAMPLES
A condition variable can be destroyed immediately after all the threads
that are blocked on it are awakened. For example, consider the follow-
ing code:
struct list {
pthread_mutex_t lm;
...
}
struct elt {
key k;
int busy;
pthread_cond_t notbusy;
...
}
/* Find a list element and reserve it. */
struct elt *
list_find(struct list *lp, key k)
{
struct elt *ep;
pthread_mutex_lock(&lp->lm);
while ((ep = find_elt(l, k) != NULL) && ep->busy)
pthread_cond_wait(&ep->notbusy, &lp->lm);
if (ep != NULL)
ep->busy = 1;
pthread_mutex_unlock(&lp->lm);
return(ep);
}
delete_elt(struct list *lp, struct elt *ep)
{
pthread_mutex_lock(&lp->lm);
assert(ep->busy);
... remove ep from list ...
ep->busy = 0; /* Paranoid. */
(A) pthread_cond_broadcast(&ep->notbusy);
pthread_mutex_unlock(&lp->lm);
(B) pthread_cond_destroy(&rp->notbusy);
free(ep);
}
In this example, the condition variable and its list element may be
freed (line B) immediately after all threads waiting for it are awak-
ened (line A), since the mutex and the code ensure that no other thread
can touch the element to be deleted.
APPLICATION USAGE
None.
RATIONALE
See pthread_mutex_init(); a similar rationale applies to condition
variables.
FUTURE DIRECTIONS
None.
SEE ALSO
pthread_cond_broadcast(), pthread_cond_signal(), pthread_cond_timed-
wait(), the Base Definitions volume of IEEE Std 1003.1-2001,
COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online
at http://www.opengroup.org/unix/online.html .
IEEE/The Open Group 2003 PTHREAD_COND_DESTROY(3P)