Linux多线程同步机制(下)

简介: Linux多线程同步机制(下)

一、读写锁

线程同步机制中的读写锁(Read-Write Lock)是一种特殊的锁机制,用于控制对共享资源的读写访问。读写锁允许多个线程同时读取共享资源,但在写操作时需要独占访问。

读写锁的基本原则是:多个线程可以同时获取读锁,但只有一个线程可以获取写锁。当有线程持有写锁时,其他线程无法获取读锁或写锁,直到写操作完成并释放写锁。

读写锁有两种状态:读模式下加锁状态(读锁),写模式下加锁状态(写锁)。读写锁只有一把。

1. 读写锁的特性:写独占,读共享。

  • 读写锁是 “ 写模式加锁 ” 时,解锁前,所有对该锁加锁的线程都会被阻塞。
  • 读写锁是 “ 读模式加锁 ” 时,如果线程以读模式 则对其加锁会成功;如果线程是以写模式加锁会阻塞。
  • 读写锁是 “ 读模式加锁 ” 时,既有试图以写模式加锁的线程,也有试图以读模式加锁的线程。那么读写锁会阻塞读模式请求。优先满足写模式加锁。读写锁并行阻塞,写锁优先级高。

2. 读写锁相关函数:

  • 初始化,pthread_rwlock_init
  • 获取读锁:使用pthread_rwlock_rdlock函数获取读锁。
  • 获取写锁:使用pthread_rwlock_wrlock函数获取写锁。
  • 释放锁:使用pthread_rwlock_unlock函数释放读锁或写锁。
  • 销毁读写锁:在不再需要时,使用pthread_rwlock_destroy函数销毁读写锁。

3. 示例代码:

下面代码设置一个写进程 和 三个读进程区访问共享资源。

(void*)i 将整数 i 转换为 void* 类型。在线程的函数中,需要将其转换回整数类型,以便使用它。可以使用 (int)arg 将 void* 转换回 int 类型。void* 是一种通用的泛型指针,可以在不关心具体类型的情况下进行转换和操作。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;   // 初始化读写锁
int data = 1;
void *write_thread (void *arg)
{
  while(1)
  {
    pthread_rwlock_wrlock(&rwlock);
    data += 1;
    printf("write : data = %d\n",data); 
    pthread_rwlock_unlock(&rwlock);
    sleep(rand() %3);         // 随机休眠,让出 cpu资源
  }
  pthread_exit(NULL);
}
void *read_thread (void *arg)
{
  int i = (int )arg;
  while(1)
  {
    pthread_rwlock_rdlock(&rwlock);
    printf("read %d : data = %d\n",i, data);  
    pthread_rwlock_unlock(&rwlock);
    sleep(rand() %3);
  }
  pthread_exit(NULL);
}
int main(void)
{
  pthread_t r_tid[5], w_tid;
  srand(time(NULL));
  // 创建多个读线程
      for (int i = 0; i < 3; i++) 
  {
     pthread_create(&r_tid[i], NULL, read_thread, (void*)i);
  }
  pthread_create(&w_tid, NULL, write_thread, NULL);
  for(int j=0;j<3;j++)
    pthread_join(r_tid[j],NULL);
  pthread_join(w_tid,NULL);
  pthread_rwlock_destroy(&rwlock);
  return 0;
}

二、条件变量

在多线程编程中,条件变量是一种用于线程之间进行通信和同步的机制。条件变量允许一个线程等待特定的条件发生,并在条件满足时通知其他线程继续执行。条件变量本身不是锁。条件变量常与互斥锁(mutex)结合使用,以实现线程之间的同步操作。

1. 相关函数:

(1) 对条件变量进行初始化,并可指定属性。通常使用默认属性,可以传入 NULL。

 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);

(2) 销毁条件变量,释放相关资源。

int pthread_cond_destroy(pthread_cond_t *cond);

当前线程等待条件变量满足,并释放关联的互斥锁。该函数会阻塞线程直至条件变量被通知。

在调用 pthread_cond_wait() 之前,必须先获得与条件变量关联的互斥锁 mutex 的锁,然后该函数会自动释放 mutex 的锁(自动 unlock),并让线程

  • 进入等待状态,直到被另一个线程通过 pthread_cond_signal() 或 pthread_cond_broadcast() 唤醒(自动 lock)。
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);

(3) 通知等待在条件变量上的一个线程,使其继续执行。如果有多个线程等待,只通知其中一个线程。

int pthread_cond_signal(pthread_cond_t *cond);

(4) 通知所有等待在条件变量上的线程,使它们都继续执行。

int pthread_cond_broadcast(pthread_cond_t *cond) ;

2. 示例代码:

描述生产者 和 消费者的关系。

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
struct msg
{
  int num;
  struct msg* pnext;
};
struct msg *head = NULL;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *producer_thread (void *arg)             // 生产者线程
{
  while(1)
  { 
    struct msg *info = malloc(sizeof(struct msg));
    info->num = rand() % 1000 + 1;
    pthread_mutex_lock(&mutex);
    info->pnext = head;
    head = info;
    pthread_mutex_unlock(&mutex);
    // 唤醒阻塞的线程
    pthread_cond_signal(&cond);     
    printf("--------------------- producer : %d\n",info->num);
    sleep(rand() % 3);
  }
  return NULL;
}
void *customer_thread (void *arg)         // 消费者线程
{
  while(1)
  {
    struct msg *info;   
    pthread_mutex_lock(&mutex);
    if(head == NULL)
    {
      pthread_cond_wait(&cond, &mutex);     // 阻塞线程,等待有数据。
    }
    info = head;
    head = info->pnext;
    pthread_mutex_unlock(&mutex);
    printf("customer : =============== %d\n",info->num);
    free(info);
    info = NULL;
    sleep(rand() % 3);
  }
  return NULL;
}
int main(void)
{
  pthread_t pid,cid;
  srand(time(NULL));            // 设置随机种子
  int ret = pthread_create(&pid, NULL, producer_thread, NULL);
  if(ret != 0)
  {
    printf("prodecer_thread_create error\n");
  }
  ret = pthread_create(&cid, NULL, customer_thread, NULL);
  if(ret != 0)
  {
    printf("consumer_thread_create error\n");
  }
  pthread_join(pid, NULL);          // 等待回收线程,获取退出线程的状态
  pthread_join(cid, NULL);
  pthread_mutex_destroy(&mutex);
  pthread_cond_destroy(&cond);
  return 0;
}

总结

相关文章
|
7天前
|
Linux
Linux——服务器时间不同步
Linux——服务器时间不同步
20 0
|
15天前
|
算法 Unix Linux
linux线程调度策略
linux线程调度策略
27 0
|
5天前
|
存储 缓存 编译器
Linux源码阅读笔记06-RCU机制和内存优化屏障
Linux源码阅读笔记06-RCU机制和内存优化屏障
|
1天前
|
存储 设计模式 NoSQL
|
10天前
|
Java 调度 开发者
Java并发编程:解锁多线程同步的奥秘
在Java的世界里,并发编程是提升应用性能的关键所在。本文将深入浅出地探讨Java中的并发工具和同步机制,带领读者从基础到进阶,逐步掌握多线程编程的核心技巧。通过实例演示,我们将一起探索如何在多线程环境下保持数据的一致性,以及如何有效利用线程池来管理资源。无论你是初学者还是有一定经验的开发者,这篇文章都将为你打开新的视野,让你对Java并发编程有更深入的理解和应用。
|
15天前
|
缓存 Linux C语言
Linux线程是如何创建的
【8月更文挑战第5天】线程不是一个完全由内核实现的机制,它是由内核态和用户态合作完成的。
|
5天前
|
Java UED
基于SpringBoot自定义线程池实现多线程执行方法,以及多线程之间的协调和同步
这篇文章介绍了在SpringBoot项目中如何自定义线程池来实现多线程执行方法,并探讨了多线程之间的协调和同步问题,提供了相关的示例代码。
31 0
|
5天前
|
NoSQL Redis
Lettuce的特性和内部实现问题之在同步调用模式下,业务线程是如何拿到结果数据的
Lettuce的特性和内部实现问题之在同步调用模式下,业务线程是如何拿到结果数据的
|
5天前
|
缓存 Linux C语言
Linux中线程是如何创建的
【8月更文挑战第15天】线程并非纯内核机制,由内核态与用户态共同实现。
|
1天前
|
Java
多线程线程同步
多线程的锁有几种方式