当前位置:编程学习 > C/C++ >>

posix多线程有感--线程高级编程(条件变量属性)

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;
}

 

 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,