マルチスレッドのプログラミング

Solaris スレッドと POSIX スレッドの API の比較

Solaris スレッド API と pthread API は、同じ問題に対する 2 つの解決策です。 それは、アプリケーションソフトウェアへの並列性構築です。どちらの API もそれ自体で完結したものですが、Solaris スレッドの関数と pthread の関数を同じプログラムの中で併用することもできます。

ただし、2 つの API は完全に一致しているわけではありません。Solaris スレッドは pthread にない関数をサポートしていて、pthread には Solaris インタフェースでサポートされない関数が含まれています。同じ関数については、機能が実質的に同じでも使用する引数が異なることがあります。

2 つの API を組み合わせて使用すれば、それぞれ他方にない機能を補い合うことができます。また、同じシステムで、Solaris スレッドだけを使用するアプリケーションを実行する一方で、pthread だけを使用するアプリケーションを実行することもできます。

API の主な相違点

Solaris スレッドと pthread は、API の動作や構文も非常によく似ています。表 6–1 に、主な相違点を示します。

表 6–1 Solaris スレッドと pthread の相違点

Solaris スレッド 

POSIX スレッド 

スレッド関数名の接頭辞が thr_ で、セマフォー関数名の接頭辞が sema_

スレッド関数名の接頭辞が pthread_ で、セマフォー関数名の接頭辞が sem_

デーモンスレッドが生成可能 

取り消しセマンティクス 

スレッドの停止と再開 

スケジューリングポリシー 

関数比較表

次の表は、Solaris スレッドの関数と pthread の関数を比較対照したものです。なお、Solaris スレッドの関数と pthread の関数が本質的に同じものとして並記されている場合でも、その引数は異なっていることがあります。

pthread または Solaris スレッドの側に相当するインタフェースがない場合は、「-」が記入されています。pthread 欄の項目で「(3RT)」が付記されているものは、POSIX.1b リアルタイム拡張機能ライブラリである librt 内の関数であり、pthread の一部ではありません。このライブラリ内の関数は、POSIX.1b リアルタイム拡張機能で指定されているほとんどのインタフェースを提供しています。

表 6–2 Solaris スレッドと POSIX pthread の比較

Solaris スレッド 

pthread 

thr_create()

pthread_create()

thr_exit()

pthread_exit()

thr_join()

pthread_join()

thr_yield()

sched_yield(3RT)()

thr_self()

pthread_self()

thr_kill()

pthread_kill()

thr_sigsetmask()

pthread_sigmask()

thr_setprio()

pthread_setschedparam()

thr_getprio()

pthread_getschedparam()

thr_setconcurrency()

pthread_setconcurrency()

thr_getconcurrency()

pthread_getconcurrency()

thr_suspend()

thr_continue()

thr_keycreate()

pthread_key_create()

pthread_key_delete()

thr_setspecific()

pthread_setspecific()

thr_getspecific()

pthread_getspecific()

pthread_once()

pthread_equal()

pthread_cancel()

pthread_testcancel()

pthread_cleanup_push()

pthread_cleanup_pop()

pthread_setcanceltype()

pthread_setcancelstate()

mutex_lock()

pthread_mutex_lock()

mutex_unlock()

pthread_mutex_unlock()

mutex_trylock()

pthread_mutex_trylock()

mutex_init()

pthread_mutex_init()

mutex_destroy()

pthread_mutex_destroy()

cond_wait()

pthread_cond_wait()

cond_timedwait()

pthread_cond_timedwait()

cond_reltimedwait()

pthread_cond_reltimedwait_np()

cond_signal()

pthread_cond_signal()

cond_broadcast()

pthread_cond_broadcast()

cond_init()

pthread_cond_init()

cond_destroy()

pthread_cond_destroy()

rwlock_init()

pthread_rwlock_init()

rwlock_destroy()

pthread_rwlock_destroy()

rw_rdlock()

pthread_rwlock_rdlock()

rw_wrlock()

pthread_rwlock_wrlock()

rw_unlock()

pthread_rwlock_unlock()

rw_tryrdlock()

pthread_rwlock_tryrdlock()

rw_trywrlock()

pthread_rwlock_trywrlock()

pthread_rwlockattr_init()

pthread_rwlockattr_destroy()

pthread_rwlockattr_getpshared()

pthread_rwlockattr_setpshared()

sema_init()

sem_init(3RT)()

sema_destroy()

sem_destroy(3RT)()

sema_wait()

sem_wait(3RT)()

sema_post()

sem_post(3RT)()

sema_trywait()

sem_trywait(3RT)()

fork1()

fork()

pthread_atfork()

forkall() (複数スレッドコピー)

pthread_mutexattr_init()

pthread_mutexattr_destroy()

mutex_init()type 引数

pthread_mutexattr_setpshared()

pthread_mutexattr_getpshared()

pthread_mutex_attr_settype()

pthread_mutex_attr_gettype()

pthread_condattr_init()

pthread_condattr_destroy()

cond_init()type 引数

pthread_condattr_setpshared()

pthread_condattr_getpshared()

pthread_attr_init()

pthread_attr_destroy()

thr_create()THR_BOUND フラグ

pthread_attr_setscope()

pthread_attr_getscope()

pthread_attr_setguardsize()

pthread_attr_getguardsize()

thr_create()stack_size 引数

pthread_attr_setstacksize()

pthread_attr_getstacksize()

thr_create()stack_addr 引数

pthread_attr_setstack()

pthread_attr_getstack()

thr_create()THR_DETACH フラグ

pthread_attr_setdetachstate()

pthread_attr_getdetachstate()

pthread_attr_setschedparam()

pthread_attr_getschedparam()

pthread_attr_setinheritsched()

pthread_attr_getinheritsched()

pthread_attr_setsschedpolicy()

pthread_attr_getschedpolicy()

この章で説明する Solaris スレッドの関数を使用するには、Solaris スレッドライブラリ (-lthread ) とリンクする必要があります。

Solaris スレッドと pthread では、関数名や引数が異なっていても、実質上の操作は変わりません。正しいインクルードファイルと関数プロトタイプを示した簡単な例を示すだけとします。Solaris スレッドの関数の戻り値が記述されていないものについては、『SunOS リファレンスマニュアル 3 : 基本ライブラリ関数』で調べてください。

Solaris 関連の関数の詳細は、pthread の関連マニュアルで類似した名前の関数を調べてください。

Solaris スレッドの関数で pthread にない機能を持つものについて、詳しく説明しています。