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 5

sem_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];
    
    // 初始化信号量,初始值为1
    sem_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;
}

在这里插入图片描述

  • 26
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

弦化

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

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

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

打赏作者

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

抵扣说明:

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

余额充值