【C++/Linux复习知识点】锁

std::mutex

最普通的互斥锁,谁竞争到改锁,谁访问临界资源

std::mutex mtx;
int shared_data = 0;

void add() {
    mtx.lock();
    shared_data++;
    mtx.unlock();
}

int main() {
    std::thread t1(add);
    std::thread t2(add);
    t1.join();
    t2.join();
    std::cout << "Shared data: " << shared_data << std::endl;
    return 0;
}

std::recursive_mutex

递归锁,普通的锁是不能递归的,一个线程加锁后,再递归,会导致死锁

std::recursive_mutex rmtx;
int shared_data = 0;

void add(int depth) {
    std::cout << std::this_thread::get_id() << std::endl;
    if (depth <= 0) {
        return;
    }
    rmtx.lock();
    ++shared_data;
    add(depth-1);
    rmtx.unlock();
}

int main() {
    std::thread t1(add, 5);
    std::thread t2(add, 5);
    t1.join();
    t2.join();
    std::cout << "Shared data: " << shared_data << std::endl;
    return 0;
}

std::timed_mutex

尝试加锁有时间限制,若超过了这个时间,就自动释放锁,并返回加锁失败

std::timed_mutex tmtx;
int shared_data = 0;

void add() {
    if (tmtx.try_lock_for(std::chrono::seconds(2))) {
        ++shared_data;
        std::this_thread::sleep_for(std::chrono::seconds (1));
        tmtx.unlock();
    } else {
        std::cout << "Timeout, could not lock mutex." << std::endl;
    }
}

int main() {
    std::thread t1(add);
    std::thread t2(add);
    t1.join();
    t2.join();
    std::cout << "shared_data: " << shared_data << std::endl;
    return 0;
}

std::shared_mutex

读写锁,可以允许多个读者读,但是写的时候,只允许有一个写者;注意读者加锁用的 shared_lock

std::shared_mutex smtx;
int shared_data = 0;

void read_data() {
    std::shared_lock<std::shared_mutex> lock(smtx);
    std::cout << "Read data: " << shared_data << std::endl;
}

void write_data(int value) {
    std::unique_lock<std::shared_mutex> lock(smtx);
    shared_data = value;
    std::cout << "Write data: " << shared_data << std::endl;
}

int main() {
    std::vector<std::thread> readers;
    for (int i = 0; i < 5; ++i) {
        readers.push_back(std::thread(read_data));
    }
    std::thread writer(write_data, 42);
    for (auto& t : readers) {
        t.join();
    }
    writer.join();
    return 0;
}

linux中的读写锁

man 3 pthread_rwlock_init

  1. 初始化读写锁
pthread_rwlock_init(pthread_rwlock_t* restrict rwlock,const pthread_rwlockattr_t* restrict attr );
  1. 销毁读写锁
pthread_rwlock_destroy(pthread_rwlock_t* rwlock);
  1. 加读锁
pthread_rwlock_rdlock(pthread_rwlock_t* rdlock);
  1. 加写锁
pthread_rwlock_wrlock(pthread_rwlock_t* rwlock);
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

int number=0;
pthread_rwlock_t rwlock;
void *write_pthread(void * arg)
{
        while(1)
        {       //加写锁
                pthread_rwlock_wrlock(&rwlock);
                number++;
                printf("write_pthread id:%ld, number is %d\n",pthread_self(),number);
                pthread_rwlock_unlock(&rwlock);
                //解锁
                sleep(1);
        }
}
void *read_pthread(void * arg)
{
        while(1)
        {       //加读锁
                pthread_rwlock_wrlock(&rwlock);
                printf("read_pthread id: %ld,number is %d\n",pthread_self(),number);
                pthread_rwlock_unlock(&rwlock);
                //解锁
               sleep(1);
        }
}

int main()
{
        pthread_t p[8];
        pthread_rwlock_init(&rwlock,NULL);
        for(int i=0;i<3;i++)
        {
                pthread_create(&p[i],NULL,write_pthread,NULL);
        }
        for(int i=3;i<8;i++)
        {
                pthread_create(&p[i],NULL,read_pthread,NULL);
                 }
        for(int j=0;j<8;j++)
        {
                pthread_join(p[j],NULL);
        }
        return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值