C++之锁

背景

在C++多线程编程中,锁是一种常用的同步原语,用于保护共享数据的访问。C++标准库提供了多种锁类型,适用于不同的使用场景。在这篇博客中,我们将介绍C++中的各种锁类型,比较它们的特点,并探讨不同锁在实际应用中的使用场景。

std::mutex

std::mutex是C++标准库中最基本的互斥锁类型,它提供了基本的锁定和解锁操作。std::mutex适用于保护简单的共享数据,如整数、指针等。

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;
int shared_data = 0;

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

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

std::recursive_mutex

std::recursive_mutex是一种递归互斥锁,它允许同一个线程多次锁定同一个互斥锁。这种锁适用于需要在递归函数中保护共享数据的场景。

#include <iostream>
#include <thread>
#include <mutex>

std::recursive_mutex rmtx;
int shared_data = 0;

void recursive_increment(int depth) {
    if (depth <= 0return;

    rmtx.lock();
    ++shared_data;
    recursive_increment(depth - 1);
    rmtx.unlock();
}

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

std::timed_mutex

std::timed_mutex是一种带超时的互斥锁,它提供了尝试加锁一定时间的功能。这种锁适用于需要在超时后执行其他操作的场景。

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>

std::timed_mutex tmtx;
int shared_data = 0;

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

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

std::shared_mutex / std::shared_timed_mutex

std::shared_mutex和std::shared_timed_mutex是一种读写锁,它们允许多个线程同时读取共享数据,但在写入数据时只允许一个线程访问。这种锁适用于读操作远多于写操作的场景。

#include <iostream>
#include <thread>
#include <shared_mutex>
#include <vector>

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;
}

各种锁的比较与使用场景

  • std::mutex:基本的互斥锁,适用于简单共享数据的保护。
  • std::recursive_mutex:递归互斥锁,适用于需要在递归函数中保护共享数据的场景。
  • std::timed_mutex:带超时的互斥锁,适用于需要在超时后执行其他操作的场景。
  • std::shared_mutex / std::shared_timed_mutex:读写锁,适用于读操作远多于写操作的场景。

最后

总结一下,在这篇博客中,我们介绍了C++中的各种锁类型,比较了它们的特点,并探讨了不同锁在实际应用中的使用场景。通过合理地选择和使用锁,我们可以有效地保护共享数据,确保多线程程序的正确性和稳定性。在进行多线程编程时,我们需要了解各种锁的特点和适用场景,以便在不同场合选择合适的锁来保护共享数据。

posted @   冰山奇迹  阅读(192)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示