4. API 比较细节 |
pthreads定义使用交互锁同步变量。从Solaris到pthreads的改变是微小的,仅增加前缀pthread_ 。所有的交互锁函数由相似的参数,除了交互锁初始化。
#include <synch.h>
int mutex_init (mutex_t *mp, int type, void *arg)
int mutex_destroy (mutex_t *mp)
int mutex_lock (mutex_t *mp)
int mutex_unlock (mutex_t *mp)
int mutex_trylock (mutex_t *mp)
#include <pthread.h>
int pthread_mutex_init (pthread_mutex_t *mp, pthread_mutexattr_t *attr)
int pthread_mutex_destroy (pthread_mutex_t *mp)
int pthread_mutex_lock (pthread_mutex_t *mp)
int pthread_mutex_unlock (pthread_mutex_t *mp)
int pthread_mutex_trylock (pthread_mutex_t *mp)
在Solaris 中
mutex_t mp;
int ret;
ret = mutex_init (&mp, USYNC_THREAD, 0); /* to be used within this process only */
在pthreads 中
如果一个交互锁变量在一个进程内共享,它可以用两种方式初始化。一种方式是用attr为NULL调用初始化函数。另一种是用一个属性对象mattr,用pshare属性的缺省值初始化此对象(PTHREAD_PROCESS_PRIVATE)。
在前一个已初始化的mattr中,也可能明显地将pshare属性设置为PTHREAD_PROCESS_PRIVATE。这些状态等价于在Solaris线索中的USYNC_THREAD状态。
pthread_mutex_t mp;
int ret;
pthread_mutexattr_t mattr;
ret = pthread_ mutex_init (&mp, NULL); /* Using a NULL argument to specify the default creation state */
或者
ret = pthread_mutexattr_init (&mattr);
ret = pthread_ mutex_init (&mp, &mattr); /* Using an attribute object initialized and left in the default state */
或者
ret = pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_PRIVATE);
ret = pthread_mutex_init (&mp, &mattr); /* Explicitly setting the attribute object to indicate PTHREAD_PROCESS_PRIVATE */
在Solaris 中
mutex_t mp;
int ret;
ret = mutex_init (&mp, USYNC_PROCESS, 0); /* to be used among all processes */
在pthreads 中
如果一个交互锁变量在进程中共享,用属性对象mattr对它初始化,此属性对象已用值PTHREAD_PROCESS_PROCESS设置。这等价于在Solaris线索中的USYNC_PROCESS状态。
mutex_t mp;
int ret;
pthread_mutexattr_t mattr;
ret = pthread_mutexattr_init (&mattr);
ret = pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED);
ret = pthread_mutex_init (&mp, &mattr); /* to be used among all processes */
在Solaris 中
mutex_t mp;
int ret;
ret = mutex_destroy(&mp); /* Mutex is destroyed */
在pthreads 中
mutex_t mp;
int ret;
ret = pthread_ mutex_destroy(&mp); /* Mutex is destroyed */
在Solaris 中
mutex_t mp;
int ret;
ret = mutex_lock (&mp); /* acquire the Mutex */
在pthreads 中
pthread_mutex_t mp;
int ret;
ret = pthread_ mutex_lock (&mp); /* acquire the Mutex */
在Solaris 中
mutex_t mp;
int ret;
ret = mutex_unlock (&mp); /* release the Mutex */
在pthreads 中
pthread_mutex_t mp;
int ret;
ret = pthread_ mutex_unlock (&mp); /* release the Mutex */
在Solaris 中
mutex_t mp;
int ret;
ret = mutex_trylock (&mp); /* try to grab Mutex */
在pthreads 中
pthread_mutex_t mp;
int ret;
ret = pthread_ mutex_trylock (&mp); /* try to grab Mutex */
Copyright: NPACT |