トップQs
タイムライン
チャット
視点
POSIXスレッド
ウィキペディアから
Remove ads
POSIXスレッド は、スレッドのPOSIX標準である。スレッド生成やスレッド操作の API を定義している。
POSIXスレッド標準を実装したライブラリは Pthreads と呼ばれることが多い。Pthreads は主にUnix系POSIXシステム(Linux や Solaris)で使われているが、Microsoft Windows 用実装も存在する。例えば、pthreads-w32 は Pthreads の API のサブセットをサポートしている[1]。
内容
要約
視点
Pthreads はC言語のデータ型、関数、定数を定義している。その実装は pthread.h というヘッダファイルとスレッドライブラリにある。スレッドのスリープは unistd.h の sleep() もしくは time.h の nanosleep() を使用する。
データ型:
pthread_t: スレッドへのハンドルpthread_attr_t: スレッド属性pthread_mutex_t: mutexpthread_mutexattr_t: mutex の属性pthread_cond_t: 条件変数pthread_condattr_t: 条件変数の属性
スレッド操作関数(引数は省略):
pthread_create(): スレッド生成pthread_exit(): 現在のスレッドを終了pthread_cancel(): 指定したスレッドの実行をキャンセル[2]pthread_join(): 現在のスレッドを他のスレッドが終了するまでブロックpthread_kill(): スレッドへのシグナル送信pthread_attr_init(): スレッド属性を初期化pthread_attr_destroy(): スレッド属性の破棄pthread_attr_getdetachstate(): detachstate属性を取得pthread_attr_setdetachstate(): datachstate属性(スレッドがjoinできるかどうか)を設定
pthread_mutex_init(): mutexロックの初期化pthread_mutex_destroy(): mutexロックの破棄pthread_mutex_lock(): mutexロックの獲得(ブロックあり)pthread_mutex_trylock(): mutexロックの獲得(ブロックしない)pthread_mutex_unlock(): mutexロックの解放pthread_mutex_getprioceiling():pthread_mutex_setprioceiling():
同期関数: 条件変数
pthread_cond_init(): 条件変数の初期化pthread_cond_destroy(): 条件変数の破棄pthread_cond_signal(): 条件変数のシグナルpthread_cond_broadcast():pthread_cond_wait(): 条件変数で待つpthread_cond_timedwait():pthread_condattr_init():pthread_condattr_destroy():pthread_condattr_getpshared():pthread_condattr_setpshared():
pthread_key_create(): スレッドローカルデータと関連付けるキーを生成pthread_key_delete(): キーの破棄pthread_getspecific(): キーを指定してスレッドローカルデータを検索pthread_setspecific(): キーとスレッドローカルデータの関連付け
Read/Writeロック
pthread_rwlock_init():pthread_rwlock_destroy():pthread_rwlock_rdlock():pthread_rwlock_wrlock():pthread_rwlock_tryrdlock():pthread_rwlock_trywrlock():pthread_rwlock_unlock():pthread_rwlockattr_init():pthread_rwlockattr_destroy():pthread_rwlockattr_getpshared():pthread_rwlockattr_setpshared():
ユーティリティ関数:
pthread_equal(): 2つのスレッドIDが等しいかどうかを調べるpthread_detach(): スレッドにリソースを解放させるpthread_self(): 自身のスレッドIDを得る
Remove ads
例
C言語で Pthreads を使ったコード例を示す。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static void *thread_func(void *vptr_args) {
int i;
for (i = 0; i < 20; i++) {
fputs(" b\n", stderr);
sleep(1);
}
return NULL;
}
int main(void) {
int i;
pthread_t thread;
if (pthread_create(&thread, NULL, thread_func, NULL) != 0) {
return EXIT_FAILURE;
}
for (i = 0; i < 20; i++) {
puts("a");
sleep(1);
}
if (pthread_join(thread, NULL) != 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
このプログラムは、'b' を1秒ごとに20回表示する新たなスレッドを生成し、同時にメインスレッドは 'a' を1秒ごとに20回表示する。出力は、2つのスレッドが並行動作することで 'a' と 'b' が入り混じったものになる。
Remove ads
関連項目
- Native POSIX Thread Library (NPTL)
参考文献
- David R. Butenhof: Programming with POSIX Threads, Addison-Wesley, ISBN 0-201-63392-2
- Bradford Nichols, Dick Buttlar, Jacqueline Proulx Farell: Pthreads Programming, O'Reilly & Associates, ISBN 1-56592-115-1
- Charles J. Northrup: Programming with UNIX Threads, John Wiley & Sons, ISBN 0-471-13751-0
- Kay A. Robbins and Steven Robbins, UNIX Systems Programming, Prentice-Hall, ISBN 0-13-042411-0
Remove ads
脚注
外部リンク
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads