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
    评论
Linux中,可以使用C语言的条件变量线程实现消费者-生产者模型。下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int count = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond_prod = PTHREAD_COND_INITIALIZER; pthread_cond_t cond_cons = PTHREAD_COND_INITIALIZER; void *producer(void *arg) { int item = 0; while (1) { pthread_mutex_lock(&mutex); // 如果缓冲区已满,则等待消费者消费 while (count == BUFFER_SIZE) { pthread_cond_wait(&cond_prod, &mutex); } buffer[count] = item; count++; printf("Producer produced item %d\n", item); // 唤醒消费者线程 pthread_cond_signal(&cond_cons); pthread_mutex_unlock(&mutex); item++; } pthread_exit(NULL); } void *consumer(void *arg) { while (1) { pthread_mutex_lock(&mutex); // 如果缓冲区为空,则等待生产者生产 while (count == 0) { pthread_cond_wait(&cond_cons, &mutex); } int item = buffer[count - 1]; count--; printf("Consumer consumed item %d\n", item); // 唤醒生产者线程 pthread_cond_signal(&cond_prod); pthread_mutex_unlock(&mutex); } pthread_exit(NULL); } int main() { pthread_t producer_thread, consumer_thread; // 创建生产者和消费者线程 pthread_create(&producer_thread, NULL, producer, NULL); pthread_create(&consumer_thread, NULL, consumer, NULL); // 等待线程结束 pthread_join(producer_thread, NULL); pthread_join(consumer_thread, NULL); return 0; } ``` 在上面的代码中,生产者线程不断地向缓冲区中生产数据,而消费者线程不断地从缓冲区中消费数据。当缓冲区满时,生产者线程会等待条件变量`cond_prod`,直到有消费者消费数据才会被唤醒。同样,当缓冲区为空时,消费者线程会等待条件变量`cond_cons`,直到有生产者生产数据才会被唤醒。 需要注意的是,在生产者和消费者线程共享的变量`count`和`buffer`需要进行互斥访问,因此使用了互斥锁`mutex`来保护共享资源的访问。 希望这个示例能帮助你理解如何在Linux中使用C语言的条件变量线程实现消费者-生产者模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

__小柒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值