博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用条件变量实现线程间同步
阅读量:4051 次
发布时间:2019-05-25

本文共 2061 字,大约阅读时间需要 6 分钟。

 

作者:曾宏安,讲师。

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

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

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

条件变量的创建和注销

int  pthread_cond_init(pthread_cond_t *restrict cond,pthread_condattr_t *restrict cond_attr); // 创建

        cond : 指向要初始化的条件变量的指针
        cond_attr : 条件变量的初始化属性
        
        pthread_cond_t cond = 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 *restrict abstime);

该函数功能同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>
        #define N 6
        
        pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
        
        pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
        void *thread_function(void *);
        int count = 0;
        int data[N];
        int main()
        {
                pthread_t thread_a;
                int i;

        if (pthread_create(&thread_a, NULL, thread_function, NULL) < 0)

                {
                        perror(“fail to pthread_create”);
                        exit(-1);
                }
                while ( 1 )
                {
                        printf(“please input 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);

        return 0;

        }
        void *thread_function(void *arg)
        {
                while ( 1 )
                {
                        int i, 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
                return NULL;
        }

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

转载地址:http://bcpci.baihongyu.com/

你可能感兴趣的文章
CodeForces 236B - Easy Number Challenge(数论:求因子个数)
查看>>
HDU 2594 Simpsons’ Hidden Talents(KMP)
查看>>
HDU 3336 Count the string(经典,KMP+DP)
查看>>
HDU 4300 Clairewd’s message(拓展KMP)
查看>>
HDU 3374 String Problem(最小最大表示法+KMP)
查看>>
HDU 4333 Revolving Digits(拓展KMP)
查看>>
zoj 3587 Marlon's String(拓展KMP+dp)
查看>>
HDU 2896 病毒侵袭(AC自动机)
查看>>
poj 3468 A Simple Problem with Integers(线段树|成段更新,区间查询)
查看>>
zoj 1610 Count the Colors(线段树,成段更新染色)
查看>>
poj 2528 Mayor's posters(线段树,离散化,成段更新染色)
查看>>
CF 6E Exposition(RMQ | 线段树,二分)
查看>>
uva 11549 - Calculator Conundrum (Floyd判圈法)
查看>>
uva 1330 - City Game
查看>>
uva 1382 - Distant Galaxy
查看>>
uva 10755 - Garbage Heap(三维最大子矩阵)
查看>>
uva 1326 Jurassic Remains(中途相遇法)
查看>>
uva 1398 - Meteor
查看>>
uva 1312 - Cricket Field
查看>>
hdu 4531 吉哥系列故事——乾坤大挪移
查看>>