1.條件變量的初始化
[cpp]
int pthread_cond_init(thread_cond_t *cond,pthread_condattr_t *attr);
int pthread_cond_init(thread_cond_t *cond,pthread_condattr_t *attr); 參數:cond 條件變量
attr 條件變量屬性
成功返回0,出錯返回錯誤編號。
注意:如果參數attr為空,那麼它將使用缺省的屬性來設置所指定的條件變量。
2.條件變量摧毀函數
[cpp]
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_destroy(pthread_cond_t *cond);成功返回0,出錯返回錯誤編號。
注意:摧毀所指定的條件變量,同時將會釋放所給它分配的資源。調用該函數的進程也並不要求等待在參數所指定的條件變量上。
3.條件變量等待函數
[cpp]
int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
int pthread_cond_timedwait(pthread_cond_t *cond,pthread_mutex_t *mytex,const struct timespec *abstime);
int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
int pthread_cond_timedwait(pthread_cond_t *cond,pthread_mutex_t *mytex,const struct timespec *abstime);參數:cond條件變量, mutex互斥鎖
注意區別:函數pthread_cond_timedwait函數類型與函數pthread_cond_wait,區別在於,如果達到或是超過所引用的參數*abstime,它將結束並返回錯誤ETIME。
typedef struct timespec
{
time_t tv_sec; //秒
long tv_nsex; //毫秒
}timespec_t;
也就是說當時間超過預設值後就返回錯誤!
4.條件變量通知函數
[cpp]
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);參數:cond 條件變量
成功返回0,出錯返回錯誤編號。
pthread_cond_signal:只喚醒一個線程。
pthread_cond_broadcast:喚醒所有線程。
注意:
當調用pthread_cond_signal時一個在相同條件變量上阻塞的線程將被解鎖。如果同時有多個線程阻塞,則由調度策略確定接收通知的線程。如果調用pthread_cond_broadcast,則將通知阻塞在這個條件變量上的所有線程。一旦被喚醒,線程仍然會要求互斥鎖。如果當前沒有線程等待通知,則上面兩種調用實際上成為一個空操作。如果參數*cond指向非法地址,則返回值EINVAL。
5.條件變量屬性的初始化/銷毀函數
[cpp]
pthread_condattr_t attr;
int pthread_condattr_init(pthread_condattr_t *attr);
int pthread_condattr_destroy(pthread_condattr_t *attr);
pthread_condattr_t attr;
int pthread_condattr_init(pthread_condattr_t *attr);
int pthread_condattr_destroy(pthread_condattr_t *attr);返回值: 若成功返回0,若失敗返回錯誤編號。
一旦某個條件變量對象被初始化了,我們就可以利用下面函數來查看或修改特定屬性了。
6.查看或修改條件變量屬性函數
[cpp]
int pthread_condattr_getpshared(pthread_condattr_t *attr,int *pshared);
int pthread_condattr_setpshared(pthread_condattr_t *attr,int pshared);
int pthread_condattr_getpshared(pthread_condattr_t *attr,int *pshared);
int pthread_condattr_setpshared(pthread_condattr_t *attr,int pshared);關於pshared的取值:
PTHREAD_PROCESS_PRIVATE(默認值):條件變量能一個進程中的線程使用。
PTHREAD_PROCESS_SHARED:條件變量能被多個進程中的線程使用。
注意:為使用一個PTHREAD_PROCESS_SHARED條件變量,必須使用一個PTHREAD_PROCESS_SHARED互斥量,因為同步使用一個條件變量的兩個線程必須使用一樣的互斥量。
[cpp]
/*
* cond_attr.c
*
* main() creates a condition variable using a non-default attributes object,
* cond_attr. If the implementation supports the pshared attribute, the
* condition variable is created "process private". (Note that, to create a
* "process shared" condition variable, the pthread_cond_t itself must be
* placed in shared memory that is accessible to all threads using the
* condition variable.)
*/
#include <pthread.h>
#include "errors.h"
pthread_cond_t cond;
int main (int argc, char *argv[])
{
pthread_condattr_t cond_attr;
int status;
status = pthread_condattr_init (&cond_attr);
if (status != 0)
err_abort (status, "Create attr");
#ifdef _POSIX_THREAD_PROCESS_SHARED
status = pthread_condattr_setpshared (
&cond_attr, PTHREAD_PROCESS_PRIVATE);
if (status != 0)
err_abort (status, "Set pshared");
#endif
status = pthread_cond_init (&cond, &cond_attr);
if (status != 0)
err_abort (status, "Init cond");
return 0;
}
/*
* cond_attr.c
*
* main() creates a condition variable using a non-default attributes object,
* cond_attr. If the implementation supports the pshared attribute, the
* condition variable is created "process private". (Note that, to create a
* "process shared" condition variable, the pthread_cond_t itself must be
* placed in shared memory that is accessible to all threads using the
* condition variable.)
*/
#include <pthread.h>
#include "errors.h"
pthread_cond_t cond;
int main (int argc, char *argv[])
{
pthread_condattr_t cond_attr;
int status;
status = pthread_condattr_init (&cond_attr);
if (status != 0)
err_abort (status, "Create attr");
#ifdef _POSIX_THREAD_PROCESS_SHARED
status = pthread_condattr_setpshared (
&cond_attr, PTHREAD_PROCESS_PRIVATE);
if (status != 0)
err_abort (status, "Set pshared");
#endif
status = pthread_cond_init (&cond, &cond_attr);
if (status != 0)
err_abort (status, "Init cond");
return 0;
}