Linux线程的实现——条件变量、读写锁

条件变量 && 读写锁 介绍

条件变量提供了一种线程间的通知机制:当某个共享数据达到某个值的时候,唤醒等待这个共享数据的线程。
这个可以想象成把线程加入到一个链表中,等待被唤醒,唤醒之后执行相应的操作

主要函数:

 #include <pthread.h>

 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);//创建条件变量

 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);//加入线程

 int pthread_cond_signal(pthread_cond_t *cond); //唤醒单个线程

 int pthread_cond_broadcast(pthread_cond_t *cond); //唤醒所有等待的线程

 int pthread_cond_destroy(pthread_cond_t *cond);//销毁条件变量

读写锁简单来说就是更加细分的互斥锁,分为读锁和写锁。
加了读锁之后,可以再次添加读锁,但是不能添加写锁;添加写锁之后,不能添加读锁和写锁。

在这里插入图片描述
主要函数:

 pthread_rwlock_rdlock(pthread_rwlock_t *lock);//加读锁
 pthread_rwlock_unlock(pthread_rwlock_t *lock);//解读锁
 pthread_rwlock_wrlock(pthread_rwlock_t *lock);//加写锁
 pthread_rwlock_unlock(pthread_rwlock_t *lock);//解写锁

一、条件变量的使用

题目:在条件变量中加入两个线程,键盘输入数据,唤醒一个线程执行读操作,当输入“end”,唤醒所有线程,结束。

   #include<stdio.h>
   #include<stdlib.h>
   #include<assert.h>
   #include<string.h>
   #include<unistd.h>
   #include<pthread.h>
   
   char buff[128] = {0};
   pthread_mutex_t mutex;
  pthread_cond_t cond;
  
  void* fun1(void* arg)
  {
      while(1)
      {
          pthread_mutex_lock(&mutex);
          pthread_cond_wait(&cond,&mutex);
          pthread_mutex_unlock(&mutex);
          if(strncmp(buff,"end",3) == 0)
          {
              break;
          }
          printf("fun1 buff = %s\n",buff);
      }
  }
  
  void* fun2(void* arg)
  {
      while(1)
      {
          pthread_mutex_lock(&mutex);
          pthread_cond_wait(&cond,&mutex);
          pthread_mutex_unlock(&mutex);
          if(strncmp(buff,"end",3) == 0)
          {

              break;
          }
          printf("fun2 buff = %s\n",buff);
      }
  }
  
  int main()
  {
      pthread_t id1,id2;
      pthread_mutex_init(&mutex,NULL);
      pthread_cond_init(&cond,NULL);
      pthread_create(&id1,NULL,fun1,NULL);
      pthread_create(&id2,NULL,fun2,NULL);
  
      while(1)
      {
          fgets(buff,128,stdin);
  
          if(strncmp(buff,"end",3) == 0)
          {
              pthread_mutex_lock(&mutex);
              pthread_cond_broadcast(&cond);
              pthread_mutex_unlock(&mutex);
              break;
          }
          else
          {
              pthread_mutex_lock(&mutex);
              pthread_cond_signal(&cond);
              pthread_mutex_unlock(&mutex);
          }
      }
  
      pthread_join(id1,NULL);
      pthread_join(id2,NULL);
      pthread_mutex_destroy(&mutex);
      pthread_cond_destroy(&cond);
  
      exit(0) ;
  }

运行结果:
在这里插入图片描述

二、读写锁的使用

题目:创建三个线程,线程一、二读,线程三写

   #include<stdio.h>
   #include<stdlib.h>
   #include<assert.h>
   #include<string.h>
   #include<unistd.h>
   #include<pthread.h>
   
   pthread_rwlock_t lock;
   void* fun1(void* arg)
  {
      while(1)
      {
          pthread_rwlock_rdlock(&lock);
          printf("fun1 read start\n");
          sleep(1);
          printf("fun1 read end\n");
          pthread_rwlock_unlock(&lock);
          sleep(1);
      }
  }
  
  void* fun2(void* arg)
  {
      while(1)
      {
          pthread_rwlock_rdlock(&lock);
          printf("fun2 read start\n");
          sleep(3);
          printf("fun2 read end\n");
          pthread_rwlock_unlock(&lock);
          sleep(1);
      }
  }
  
  void* fun3(void* arg)
  {
      while(1)
      {
          pthread_rwlock_wrlock(&lock);
          printf("fun3 write lock\n");
          sleep(4);
          printf("fun3 write end\n");
          pthread_rwlock_unlock(&lock);
          sleep(1);
      }
  }
  
  int main()
  {
      pthread_rwlock_init(&lock,NULL);
      pthread_t id1,id2,id3;
  
      pthread_create(&id1,NULL,fun1,NULL);
      pthread_create(&id3,NULL,fun2,NULL);
      pthread_create(&id3,NULL,fun3,NULL);
  
      pthread_join(id1,NULL);
      pthread_join(id2,NULL);
      pthread_join(id3,NULL);
  }

运行结果:

在这里插入图片描述
这里因为写了一个简单的程序,是一个死循环,所以CTRL+C结束了进程。

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__小柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值