利用条件变量实现线程间同步

来源:互联网 发布:java零基础 入门课程 编辑:程序博客网 时间:2024/05/16 09:30

 

作者:曾宏安,华清远见嵌入式学院讲师。

我们在编写多线程程序时经常需要在线程之间实现通信,常见的机制有信号量和互斥锁。这里再向大家介绍一种用于实现线程间同步的机制——条件变量。

条件变量可以使线程睡眠等待直到某个条件满足为止。条件变量基本使用操作有两种:一、当判断条件不满足时,某些线程睡眠在相应的条件变量上;二、某些线程改变了条件,唤醒睡眠在条件变量上的其他线程。

为了在判断或是改变条件时防止竞争,条件变量通常和互斥锁结合在一起使用。条件变量类型为pthread_cond_t,互斥锁类型为pthread_mutex_t。

条件变量的创建和注销

int  pthread_cond_init(pthread_cond_t *restrictcond,pthread_condattr_t *restrict cond_attr); // 创建
        cond: 指向要初始化的条件变量的指针
        cond_attr: 条件变量的初始化属性
        
        pthread_cond_tcond = PTHREAD_COND_INITIALIZER;

int pthread_cond_destroy(pthread_cond_t *cond); // 注销

条件变量的等待和激活

int pthread_cond_wait(pthread_cond_t *restrict cond,
        pthread_mutex_t*restrict mutex);

该函数使当前线程释放互斥锁mutex并睡眠在条件变量cond上,正确返回时当前线程获得互斥锁mutex

int pthread_cond_timewait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,const struct timespec *restrictabstime);

该函数功能同pthread_cond_wait,但睡眠时间不超过abstime,正确返回时当前线程获得互斥锁mutex

int pthread_cond_signal(pthread_cond_t *cond);
        该函数唤醒等待在条件变量cond上的某个线程

int pthread_cond_broadcast(pthread_cond_t *cond);
        该函数唤醒等待在条件变量cond上的所有线程

下面我们看一个例子,了解一下条件变量的用法

#include <stdio.h>
        #include<stdlib.h>
        #include<unistd.h>
        #include<pthread.h>
        #defineN 6
        
        pthread_mutex_tmutex = PTHREAD_MUTEX_INITIALIZER;
        
        pthread_cond_tcond = PTHREAD_COND_INITIALIZER;
        void*thread_function(void *);
        intcount = 0;
        intdata[N];
        intmain()
        {
                pthread_tthread_a;
                inti;

        if(pthread_create(&thread_a, NULL, thread_function,NULL) < 0)
                {
                        perror(“failto pthread_create”);
                        exit(-1);
                }
                while( 1 )
                {
                        printf(“pleaseinput number : “);
                        scanf(“%d”,&i);
                        if(i = = 0) break;
                        pthread_mutex_lock(&mutex);
                        data[count++]= i;
                        if(count == N) pthread_cond_signal(&cond);
                        pthread_mutex_unlock(&mutex);
                        usleep(100000);
                }
                pthread_mutex_destroy(&mutex);
                pthread_cond_destroy(&cond);

        return0;
        }
        void*thread_function(void *arg)
        {
                while( 1 )
                {
                        inti, sum;
                        pthread_mutex_lock(&mutex);
                        if(count < N)
                        {
                                pthread_cond_wait(&cond,&mutex);
                        }
                        for(i=0,sum=0; i<N; i++) sum += data[i];
                        printf(“average= %d\n”, sum/N);
                        count= 0;
                        pthread_mutex_unlock(&mutex);
                }// end while
                returnNULL;
        }

在上面的代码中,主线程从键盘读取整数。每读入6个数后,唤醒另外一个线程统计并打印平均数。输入0退出程序。

0 0
原创粉丝点击