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

废话就不多说了,开始。。。

    1.条件变量的初始化

int pthread_cond_init(thread_cond_t *cond,pthread_condattr_t *attr);

  数参:cond  条件变量
            attr     条件变量性属
 功成返回0,犯错返回错误编号。
   
注意:如果数参attr为空,那么它将应用省缺的性属来设置所指定的条件变量。

    2.条件变量毁摧函数

int pthread_cond_destroy(pthread_cond_t *cond);

    功成返回0,犯错返回错误编号。

    注意:毁摧所指定的条件变量,同时将会释放所给它分配的源资。调用该函数的进程也不并求要等待在数参所指定的条件变量上。

    3.条件变量等待函数

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.条件变量通知函数

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.条件变量性属的初始化/销毁函数

pthread_condattr_t attr;
int pthread_condattr_init(pthread_condattr_t *attr);
int pthread_condattr_destroy(pthread_condattr_t *attr);

    返回值: 若功成返回0,若失败返回错误编号。 
一旦某个条件变量对象被初始化了,我们就能够利用面下函数来看查或修改特定性属了。

    6.看查或修改条件变量性属函数

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互斥量,因为同步应用一个条件变量的两个程线必须应用一样的互斥量。

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

    



文章结束给大家分享下程序员的一些笑话语录: 联想——对内高价,补贴对外倾销的伟大“民族”企业。

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3069772.html