EN JA
THRD_CREATE(3)
THRD_CREATE(3) FreeBSD Library Functions Manual THRD_CREATE(3)

名称

call_once, cnd_broadcast, cnd_destroy, cnd_init, cnd_signal, cnd_timedwait, cnd_wait, mtx_destroy, mtx_init, mtx_lock, mtx_timedlock, mtx_trylock, mtx_unlock, thrd_create, thrd_current, thrd_detach, thrd_equal, thrd_exit, thrd_join, thrd_sleep, thrd_yield, tss_create, tss_delete, tss_get, tss_setC11 スレッドインタフェース

ライブラリ

library “libstdthreads”

書式

#include < threads.h>

void
call_once( once_flag *flag, void (*func)(void));

int
cnd_broadcast( cnd_t *cond);

void
cnd_destroy( cnd_t *cond);

int
cnd_init( cnd_t *cond);

int
cnd_signal( cnd_t *cond);

int
cnd_timedwait( cnd_t * restrict cond, mtx_t * restrict mtx, const struct timespec * restrict ts);

int
cnd_wait( cnd_t *cond, mtx_t *mtx);

void
mtx_destroy( mtx_t *mtx);

int
mtx_init( mtx_t *mtx, int type);

int
mtx_lock( mtx_t *mtx);

int
mtx_timedlock( mtx_t * restrict mtx, const struct timespec * restrict ts);

int
mtx_trylock( mtx_t *mtx);

int
mtx_unlock( mtx_t *mtx);

int
thrd_create( thrd_t *thr, int (*func)(void *), void *arg);

thrd_t
thrd_current( void);

int
thrd_detach( thrd_t thr);

int
thrd_equal( thrd_t thr0, thrd_t thr1);

_Noreturn void
thrd_exit( int res);

int
thrd_join( thrd_t thr, int *res);

int
thrd_sleep( const struct timespec *duration, struct timespec *remaining);

void
thrd_yield( void);

int
tss_create( tss_t *key, void (*dtor)(void *));

void
tss_delete( tss_t key);

void *
tss_get( tss_t key);

int
tss_set( tss_t key, void *val);

解説

ISO/IEC 9899:2011 (“ISO C11”) の時点で、C 標準は、マルチスレッドのアプリケーションを書くための API を含んでいます。 POSIX.1 は、事実上任意のマルチスレッドのアプリケーションによって使用される、スレッド化された API を既に含んでいるので、 C 標準によって提供されるインタフェースは、不必要であると考えることができます。

したがって、この実装は、スレッド化されたインタフェースは、既存のインタフェースの上のライトウェート (軽量) の層として実装されます。これらのルーチンがマップされる関数は、次のテーブルにリストされています。より詳細については、POSIX 相当の関数の文書を参照してください。

関数 POSIX 相当
call_once() pthread_once(3)
cnd_broadcast() pthread_cond_broadcast(3)
cnd_destroy() pthread_cond_destroy(3)
cnd_init() pthread_cond_init(3)
cnd_signal() pthread_cond_signal(3)
cnd_timedwait() pthread_cond_timedwait(3)
cnd_wait() pthread_cond_wait(3)
mtx_destroy() pthread_mutex_destroy(3)
mtx_init() pthread_mutex_init(3)
mtx_lock() pthread_mutex_lock(3)
mtx_timedlock() pthread_mutex_timedlock(3)
mtx_trylock() pthread_mutex_trylock(3)
mtx_unlock() pthread_mutex_unlock(3)
thrd_create() pthread_create(3)
thrd_current() pthread_self(3)
thrd_detach() pthread_detach(3)
thrd_equal() pthread_equal(3)
thrd_exit() pthread_exit(3)
thrd_join() pthread_join(3)
thrd_sleep() nanosleep(2)
thrd_yield() pthread_yield(3)
tss_create() pthread_key_create(3)
tss_delete() pthread_key_delete(3)
tss_get() pthread_getspecific(3)
tss_set() pthread_setspecific(3)

POSIX 相当との相違点

thrd_exit() 関数は、 thrd_join() を呼び出すスレッドへの整数値を返すのに対して、 pthread_exit() 関数は、ポインタを使用します。

mtx_init() によって作成されたミューテックは、 mtx_timedlock() をサポートするミューテックの間で区別するために、 type (タイプ) mtx_plain または mtx_timed を指定できます。このタイプは、再帰的な獲得を許可するミューテックを作成するために mtx_recursive論理和 (OR) することができます。これらの特性は、通常 pthread_mutex_init() の attr パラメータを使用して、設定されます。

戻り値

成功するなら、 cnd_broadcast(), cnd_init(), cnd_signal(), cnd_timedwait(), cnd_wait(), mtx_init(), mtx_lock(), mtx_timedlock(), mtx_trylock(), mtx_unlock(), thrd_create(), thrd_detach(), thrd_equal(), thrd_join(), thrd_sleep(), tss_create() と tss_set() 関数は、 thrd_success を返します。そうでなければ、エラーコードが、エラーを示すために返されます。

thrd_current() 関数は、呼び出しているスレッドのスレッド ID を返します。

tss_get() 関数は、与えられた key に関連したスレッド特有のデータ値を返します。スレッドに特有のデータ値が key, に関連していないなら、値 NULL が返されます。

エラー

cnd_init() と thrd_create() 関数は、次の場合に失敗します:
thrd_nomem
システムには十分なメモリがありません。

cnd_timedwait() と mtx_timedlock() 関数は、次の場合に失敗します:

thrd_timedout
システム時間が、操作が完了する前に、 ts で指定された時間に到達したか、または超過しました。

mtx_trylock() 関数は、次の場合に失敗します:

thrd_busy
ミューテックが既にロックされています。

他のすべての場合に、これらの関数は、一般的なエラーコード thrd_error を返して失敗するかもしれません。

関連項目

nanosleep(2), pthread(3)

規格

これらの関数は、 ISO/IEC 9899:2011 (“ISO C11”) に適合するはずです。

歴史

これらの関数は、 FreeBSD 10.0 で登場しました。

作者

Ed Schouten <ed@FreeBSD.org>
December 26, 2011 FreeBSD