当前位置: 首页 > news >正文

Linux 线程安全 (2)

文章目录

  • 线程同步概念
  • 条件变量使用
  • 生产消费模型
  • 信号量的使用
  • 读写锁的使用

Linux 线程安全 (1)

线程同步概念

竞态条件:因为时序问题,而导致程序异常.

饥饿问题:只使用互相锁保证线程安全时,锁资源总被某一个线程占用的情况。

线程同步:线程同步是指在多线程编程中,为了保证临界资源的正确访问和避免竞态条件,需要协调和控制线程之间的执行顺序和互斥访问。让线程能够按照某种特定的顺序访问临界资源,从而有效避免饥饿问题。常见的线程同步机制包括互斥锁(Mutex)、信号量(Semaphore)、条件变量(Condition Variable)等。

条件变量使用

在多线程编程中,有需要等待某个资源就绪,线程才能开始正常运行的需求。
例如一个线程访问队列时,发现队列为空,它只能等待,只到其它线程将一个节点添加到队列中。这种情况就需要用到条件变量。

条件变量是一种线程同步的高级机制,它可以实现线程的等待和唤醒操作。条件变量通常与互斥锁一起使用,当某个条件不满足时,线程可以调用条件变量的等待操作进入等待状态,当条件满足时,其他线程可以调用条件变量的唤醒操作来唤醒等待的线程。

// 1.初始化条件变量 
//cond为要初始化的条件变量
int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr);
//2.等待条件满足 
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
//3.唤醒等待
//唤醒所有在等待的线程
int pthread_cond_broadcast(pthread_cond_t *cond);
//随机唤醒一个真在等待的线程
int pthread_cond_signal(pthread_cond_t *cond);

例子

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>pthread_cond_t cond;
pthread_mutex_t mutex;void *r1( void *arg )
{while ( 1 ){pthread_cond_wait(&cond, &mutex);printf("活动\n");}
}void *r2(void *arg )
{while ( 1 ) {pthread_cond_signal(&cond);sleep(1);}
}int main( void )
{pthread_t t1, t2;pthread_cond_init(&cond, NULL);pthread_mutex_init(&mutex, NULL);pthread_create(&t1, NULL, r1, NULL);pthread_create(&t2, NULL, r2, NULL);pthread_join(t1, NULL);pthread_join(t2, NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);
}

条件变量实际使用规范
①等待条件代码

pthread_mutex_lock(&mutex);
while (条件为假)
pthread_cond_wait(cond, mutex);
修改条件
pthread_mutex_unlock(&mutex);

②给条件发送信号代码

pthread_mutex_lock(&mutex);
设置条件为真
pthread_cond_signal(cond);
pthread_mutex_unlock(&mutex);

"条件” 可以理解为需要等待就绪的资源。
条件为假:线程代码运行需要的资源未就绪。
条件为真:线程代码运行需要的资源就绪。
修改条件:使用资源。

生产消费模型

产者消费者模型是一种常见的并发编程模型,用于解决多线程环境下的生产者和消费者之间的协作问题。在这个模型中,生产者负责生产数据,并将数据放入共享的缓冲区中,而消费者则负责从缓冲区中取出数据并进行消费。

该模型的基本思想是通过一个共享的缓冲区来实现生产者和消费者之间的同步与通信。生产者在生产数据之前会检查缓冲区是否已满,如果已满则等待,直到有空闲位置。而消费者在消费数据之前会检查缓冲区是否为空,如果为空则等待,直到有数据可供消费。

为了实现这种同步与通信,可以使用互斥锁(mutex)来保护缓冲区的访问,以及条件变量(condition variable)来实现生产者和消费者之间的等待和通知机制。

在这里插入图片描述

信号量的使用

此处的信号量使用指的是对POSIX信号量的使用,POSIX信号量可以用于线程间同步。

//1.初始化信号量
//pshared=0表示线程间共享信号量
//value 表示信号量的初始值
int sem_init(sem_t *sem, int pshared, unsigned int value);
//2.等待信号量,会将信号量的值减
int sem_wait(sem_t *sem);
//3.发布信号量,表示资源使用完毕,可以归还资源了。将信号量值加1
int sem_post(sem_t *sem);
//4.销毁信号量
int sem_destroy(sem_t *sem);
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>#define NUM_THREADS 5sem_t semaphore;void* thread_function(void* arg) 
{int thread_id = *(int*)arg;printf("Thread %d is waiting...\n", thread_id);sem_wait(&semaphore);printf("Thread %d has acquired the semaphore.\n", thread_id);// 模拟线程执行一些任务sleep(2);printf("Thread %d is releasing the semaphore.\n", thread_id);sem_post(&semaphore);pthread_exit(NULL);
}int main() 
{pthread_t threads[NUM_THREADS];int thread_ids[NUM_THREADS];// 初始化信号量,初始值为1sem_init(&semaphore, 0, 1);// 创建多个线程for (int i = 0; i < NUM_THREADS; i++) {thread_ids[i] = i;pthread_create(&threads[i], NULL, thread_function, &thread_ids[i]);}// 等待所有线程结束for (int i = 0; i < NUM_THREADS; i++) {pthread_join(threads[i], NULL);}// 销毁信号量sem_destroy(&semaphore);return 0;
}

读写锁的使用

在编写多线程的时候,有一种情况是十分常见的。那就是,有些公共数据修改的机会比较少。相比较改写,它们读的机会反而高的多。通常而言,在读的过程中,往往伴随着查找的操作,中间耗时很长。给这种代码段加锁,会极大地降低我们程序的效率。 为了专门处理这种情况,有了读写锁。

使用

// 1. 设置读写优先级
// attr指向要设置属性的读写锁的指针
// pref 共有3种选择
//PTHREAD_RWLOCK_PREFER_READER_NP:优先选择读者。
//PTHREAD_RWLOCK_PREFER_WRITER_NP:优先选择写者。
//PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP:优先选择非递归写者。
int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, 
int pref);//2. 初始化
//rwlock用于指定要初始化的读写锁对象。
//attr设置为nullptr
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);//3. 加锁和解锁/*pthread_rwlock_rdlock函数用于获取读写锁的读取锁定。
读取锁定允许多个线程同时持有锁,只要没有线程持有写入锁。
如果有线程持有写入锁,则其他线程尝试获取读取锁会被阻塞,直到写入锁被释放。*/
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);/*
pthread_rwlock_wrlock函数用于获取读写锁的写入锁定。
写入锁定是独占的,只能由一个线程持有。如果有线程持有读取锁或写入锁,
则其他线程尝试获取写入锁会被阻塞,直到所有读取锁和写入锁都被释放
*/
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);//用于释放读写锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

实际使用:

#include <stdio.h>
#include <pthread.h>// 共享资源
int shared_data = 0;// 读写锁
pthread_rwlock_t rwlock;// 读线程函数
void* reader_function(void* arg) 
{int thread_id = *(int*)arg;while (1) {// 获取读锁pthread_rwlock_rdlock(&rwlock);// 读取共享资源printf("Reader %d reads shared data: %d\n", thread_id, shared_data);// 释放读锁pthread_rwlock_unlock(&rwlock);// 等待一段时间sleep(1);}pthread_exit(NULL);
}// 写线程函数
void* writer_function(void* arg) 
{int thread_id = *(int*)arg;while (1) {// 获取写锁pthread_rwlock_wrlock(&rwlock);// 修改共享资源shared_data++;printf("Writer %d updates shared data: %d\n", thread_id, shared_data);// 释放写锁pthread_rwlock_unlock(&rwlock);// 等待一段时间sleep(2);}pthread_exit(NULL);
}int main() 
{pthread_t readers[3];pthread_t writers[2];int reader_ids[3] = {1, 2, 3};int writer_ids[2] = {1, 2};// 初始化读写锁pthread_rwlock_init(&rwlock, NULL);// 创建读线程for (int i = 0; i < 3; i++) {pthread_create(&readers[i], NULL, reader_function, &reader_ids[i]);}// 创建写线程for (int i = 0; i < 2; i++) {pthread_create(&writers[i], NULL, writer_function, &writer_ids[i]);}// 等待读线程结束for (int i = 0; i < 3; i++) {pthread_join(readers[i], NULL);}// 等待写线程结束for (int i = 0; i < 2; i++) {pthread_join(writers[i], NULL);}// 销毁读写锁pthread_rwlock_destroy(&rwlock);return 0;
}

在这里插入图片描述

http://www.lryc.cn/news/269373.html

相关文章:

  • 异或运算^简述
  • Google Play上架:2023年度总结报告
  • JAVA进化史: JDK10特性及说明
  • 第二百三十四回
  • {MySQL} 数据库约束 表的关系 新增删除 修改 查询
  • 【JVM】虚拟机的组成+字节码文件组成+类的生命周期
  • pip 下载太慢的解决办法,pip换国内源,pip换源
  • OKCC语音机器人的人机耦合来啦
  • 有序数组的平方
  • Java虚拟机中的垃圾回收
  • Vscode新手安装与使用
  • 以元旦为题的诗词(二)
  • 饥荒Mod 开发(二一):超大便携背包,超大物品栏,永久保鲜
  • js 七种继承方法
  • Unity Shader 实现X光效果
  • Android Camera相关类功能整理
  • 3、Git分支操作与团队协作
  • Linux网卡配置
  • wireshark access/trunk/hybrid报文分析
  • C++ Primer Plus----第十二章--类和动态内存分布
  • 清理 Oracle Arch 目录中的日志文件
  • PicGo+GitHub搭建免费图床
  • 免费在线客服软件推荐:经济实用的客户沟通解决方案
  • leetcode344. 反转字符串
  • 【js自定义鼠标样式】【js自定义鼠标动画】
  • Linux开发工具——gdb篇
  • spring状态机
  • K8S异常处理
  • 【挑战全网最易懂】深度强化学习 --- 零基础指南
  • WPF RelativeSource