4. API 比较细节 |
pthreads条件变量是标准的,而且相似于那些在Solaris线索实现中所提供的。从Solaris到pthreads的改变是微小的,仅增加前缀pthread_ 。所有的条件变量函数由相似的参数,除了交互锁初始化。
#include <synch.h>
int cond_init (cond_t *cv, int type, int arg)
int cond_destroy (cond_t *cv)
int cond_wait (cond_t *cv, mutex_t *mp)
int cond_timedwait (cond_t *cv, mutex_t *mp, timestruct_t abstime)
int cond_signal (cond_t *cv) int cond_broadcast (cond_t *cv)
#include <pthread.h>
#include <time.h>
int pthread_cond_init (pthread_cond_t *cv, pthread_condattr_t *attr)
int pthread_cond_destroy (pthread_cond_t *cv)
int pthread_cond_wait (pthread_cond_t *cv, pthread_mutex_t *mp)
int pthread_cond_timedwait (pthread_cond_t *cv, pthread_mutex_t *mp, timestruct_t abstime)
int pthread_cond_signal (pthread_cond_t *cv) int pthread_cond_broadcast (pthread_cond_t *cv)
在Solaris 中
cond_t cv;
int ret;
ret = cond_init (cv, USYNC_THREAD, 0); /* to be used within this process only */
在pthreads 中
如果一个条件变量在一个进程内共享,它可以用两种方式初始化。一种方式是用attr为NULL调用初始化函数。另一种是用一个属性对象cattr,用pshare属性的缺省值初始化此对象(PTHREAD_PROCESS_PRIVATE)。
在前一个已初始化的cattr中, 也可能明显地将pshare属性设置为PTHREAD_PROCESS_PRIVATE。
pthread_cond_t cv;
int ret;
pthread_condattr_t cattr;
ret = pthread_ cond_init (&cv, NULL); /* Using a NULL argument to specify the default creation state */
或者
ret = pthread_condattr_t (&cattr);
ret = pthread_ cond_init (&cv, &cattr); /* Using an attribute object initialized and left in the default state */
或者
ret = pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_PRIVATE);
ret = pthread_cond_init (&cv, &cattr); /* Explicitly setting the attribute object to indicate PTHREAD_PROCESS_PRIVATE */
在Solaris 中
cond_t cv;
int ret;
ret = cond_init (&cv, USYNC_PROCESS, 0); /* to be used among all processes */
在pthreads 中
如果一个条件变量在进程中共享,用属性对象cattr对它初始化,此属性对象已用值PTHREAD_PROCESS_PROCESS设置。这等价于在Solaris线索中的USYNC_PROCESS状态。
pthread_cond_t cv;
int ret;
pthread_condattr_t cattr;
ret = pthread_condattr_init (&cattr);
ret = pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED);
ret = pthread_cond_init (&cv, &cattr); /* to be used among all processes */
在Solaris 中
cond_t cv;
int ret;
ret = cond_destroy (&cv); /* Condition variable is destroyed */
在pthreads 中
pthread_cond_t cv;
int ret;
ret =pthread_ cond_destroy (&cv); /* Condition variable is destroyed */
在Solaris 中
cond_t cv;
mutex_t mp;
int ret;
ret = cond_wait (&cv, &mp); /* wait on condition variable */
在pthreads 中
pthread_cond_t cv;
pthread_mutex_t mp;
int ret;
ret = pthread_cond_wait (&cv, &mp); /* wait on condition variable */
在Solaris 中
cond_t cv;
mutex_t mp;
timestruct_t abstime;
int ret; ret = cond_timedwait (&cv, &mp, &abstime); /* wait on condition variable */
在pthreads 中
pthread_cond_t cv;
pthread_mutex_t mp;
timestruct_t abstime;
int ret;
ret = pthread_cond_timedwait (&cv, &mp, &abstime); /* wait on condition variable */
在Solaris 中
cond_t cv;
int ret;
ret = cond_signal (&cv); /* one condition variable is signaled */
在pthreads 中
pthread_cond_t cv;
int ret;
ret = pthread_cond_signal (&cv); /* one condition variable is signaled */
在Solaris 中
cond_t cv;
int ret;
ret = cond_broadcast(&cv); /* all condition variables are signaled */
在pthreads 中
pthread_cond_t cv;
int ret;
ret = pthread_cond_broadcast(&cv); /* all condition variables are signaled */
Copyright: NPACT |