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

上海昆山网站公司浙江微信网站建设报价

上海昆山网站公司,浙江微信网站建设报价,好的网站具备的条件,中级网络工程师含金量1、背景 在 C 多线程编程中,同步 和 互斥 是至关重要的概念。C 标准库提供了多种同步机制,其中 std::condition_variable_any、std::lock_guard 和 std::unique_lock 是经常被用到的工具。本文将详细介绍这三者的用途、区别、适用场景,并通过…

1、背景

在 C++ 多线程编程中,同步 和 互斥 是至关重要的概念。C++ 标准库提供了多种同步机制,其中 std::condition_variable_any、std::lock_guard 和 std::unique_lock 是经常被用到的工具。本文将详细介绍这三者的用途、区别、适用场景,并通过示例展示如何正确使用它们。

2、std::lock_guard

std::lock_guard 是 C++ 提供的一个 RAII(资源获取即初始化)风格的互斥锁管理器,用于在作用域内 自动管理 std::mutex 的加锁和解锁,确保不会发生死锁或者忘记解锁的问题,它是一种轻量级自动管理锁。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void print_message(const std::string& msg) {std::lock_guard<std::mutex> lock(mtx); // 自动加锁,作用域结束时自动解锁std::cout << msg << std::endl;
}int main() {std::thread t1(print_message, "Hello from thread 1");std::thread t2(print_message, "Hello from thread 2");t1.join();t2.join();return 0;
}

该锁主要适用于短生命周期的互斥操作。

3、std::unique_lock

与std::lock_guard相比,std::unique_lock支持更加灵活的互斥锁管理。主要有以下几个功能:

  • 支持 defer_lock(延迟加锁),允许在稍后手动调用 lock();
  • 支持手动 unlock(),适用于更复杂的同步场景。
#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void worker() {std::unique_lock<std::mutex> lock(mtx, std::defer_lock); // 不自动加锁std::cout << "Before locking...\n";lock.lock(); // 需要时手动加锁std::cout << "Worker thread acquired lock\n";lock.unlock(); // 需要时手动解锁std::cout << "Worker thread released lock\n";
}int main() {std::thread t(worker);t.join();return 0;
}
  • 支持 try_lock,尝试加锁但不阻塞;
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>std::mutex mtx;void worker() {std::unique_lock<std::mutex> lock(mtx, std::try_to_lock); // 尝试加锁if (lock.owns_lock()) { // 判断是否成功加锁std::cout << "Worker acquired lock\n";} else {std::cout << "Worker failed to acquire lock\n";}
}int main() {std::unique_lock<std::mutex> main_lock(mtx); // 先加锁,让 worker 线程无法获取锁std::thread t(worker);std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 模拟主线程持锁一段时间main_lock.unlock(); // 释放锁t.join();return 0;
}
  • 支持 timed_lock,尝试在一定时间内加锁;
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>std::mutex mtx;void worker() {std::unique_lock<std::mutex> lock(mtx, std::defer_lock); // 延迟加锁if (lock.try_lock_for(std::chrono::seconds(2))) { // 等待最多2秒尝试加锁std::cout << "Worker acquired lock\n";} else {std::cout << "Worker failed to acquire lock within timeout\n";}
}int main() {std::unique_lock<std::mutex> main_lock(mtx); // 先加锁std::thread t(worker);std::this_thread::sleep_for(std::chrono::seconds(3)); // 持锁超过 worker 的等待时间main_lock.unlock(); // 释放锁t.join();return 0;
}

4、std::condition_variable_any

std::condition_variable 只能和 std::unique_lockstd::mutex 结合使用,但是 std::condition_variable_any 可以适配 std::shared_mutex,从而实现 读写者模型。

#include <iostream>
#include <thread>
#include <shared_mutex>
#include <condition_variable>std::shared_mutex rw_mutex;
std::condition_variable_any cv;
bool data_ready = false;
int data = 0;// 读线程(多个线程可以共享读取)
void reader(int id) {std::shared_lock<std::shared_mutex> lock(rw_mutex);cv.wait(lock, [] { return data_ready; }); // 等待数据就绪std::cout << "Reader " << id << " read data: " << data << std::endl;
}// 写线程(独占写入)
void writer() {std::this_thread::sleep_for(std::chrono::seconds(2));{std::unique_lock<std::shared_mutex> lock(rw_mutex);data = 42;data_ready = true;}cv.notify_all(); // 唤醒所有等待的读线程
}int main() {std::thread readers[3] = {std::thread(reader, 1),std::thread(reader, 2),std::thread(reader, 3)};std::thread writer_thread(writer);for (auto& r : readers) r.join();writer_thread.join();return 0;
}
  • std::condition_variable_any 与 std::condition_variable 的区别
特性std::condition_variablestd::condition_variable_any
兼容锁类型只能配合std::unique_lock<std::mutx>兼容所有符合Lockable规范的锁如std::unique_lock<std::shared_mutex>
适用范围只能用于std::mutex适用于std::mutex、std::shared_mutex等
适用场景经典生产者-消费者模型适用于读写锁等更多场景
线程等待允许多个线程等待同一条件允许多个线程等待同一条件

5、结论

  • 如果你只需要在作用域内加锁并确保自动释放,使用 std::lock_guard。
  • 如果你需要更灵活的锁管理,如手动解锁或尝试加锁,使用 std::unique_lock。
  • 如果你需要线程间的条件同步,使用 std::condition_variable_any或者std::condition_variable,再根据std::mutex和std::shared_mutex哪个可以满足需求决定使用那一个条件变量,std::condition_variable_any比std::condition_variable功能更加强大,因此它的性能开销应该比std::condition_variable更大,因此如果std::condition_variable可以满足需要,最好使用std::condition_variable,否则使用std::condition_variable_any。

文章转载自:

http://ArZ2pxBu.ydytt.cn
http://zsxE9lPB.ydytt.cn
http://CH8ke7Gj.ydytt.cn
http://4iAQw4mZ.ydytt.cn
http://LCKORtq2.ydytt.cn
http://sUWvouJH.ydytt.cn
http://DUkdZYAL.ydytt.cn
http://2tk1Yp7a.ydytt.cn
http://zLdU2BWU.ydytt.cn
http://yLOrLgpX.ydytt.cn
http://sxsdmaF0.ydytt.cn
http://kS4YVsoT.ydytt.cn
http://jjxS19xY.ydytt.cn
http://zRvWWN7A.ydytt.cn
http://keoMnBbk.ydytt.cn
http://eA2JOTpW.ydytt.cn
http://77VfxYTy.ydytt.cn
http://G2CbdQIR.ydytt.cn
http://khAZXyQG.ydytt.cn
http://LtVKOeQq.ydytt.cn
http://nmQYAMCS.ydytt.cn
http://19nIoX9G.ydytt.cn
http://HbZziBl8.ydytt.cn
http://Proy0yMQ.ydytt.cn
http://ngRgq8az.ydytt.cn
http://k1EaGeXT.ydytt.cn
http://3spNkMDQ.ydytt.cn
http://xtGtottI.ydytt.cn
http://RdrFBIyX.ydytt.cn
http://rQkJynSJ.ydytt.cn
http://www.dtcms.com/wzjs/732137.html

相关文章:

  • 做电商平台网站有哪些内容校园网站开发背景
  • 中国网站设计欣赏珠海中国建设银行招聘信息网站
  • 衡水哪家制作网站好足球比赛直播在线观看
  • 站点推广西安建筑信息平台
  • 响应式购物网站设计大连网站开发招聘
  • hltm 做网站教程怎么查询网站是否收录
  • 织梦cms 网站栏目管理网站 美化
  • 主机托管网站南宁网站开发建设
  • 手机建网站教程wordpress多用户模版
  • 用商标做网站名字云南住房和建设厅网站
  • 网站模板和后台巴彦淖尔网站网站建设
  • 线上外贸平台有哪些seo快速排名百度首页
  • 定制专业网站网站模板 整站源码下载
  • 自建网站备案通过后怎么做浙江建设干部学校网站
  • 怎样做付费下载的网站江西做网站找谁
  • seo入门基础教程郑州做网络优化的公司
  • 网站推广网站策划西安百度seo推广
  • 广州小网站建设百度投放广告怎么收费
  • 最炫的网站建设银行个人客户
  • 汝州住房和城乡建设局新网站心理医生免费咨询
  • 网站建设岗位廉政风险防控手机做app任务的网站
  • 住房城乡建设部网站办事大厅久其软件公司网站
  • asp.net网站开发实例教程漯河市源汇区建设局网站
  • 动画形式的h5在哪个网站做哈尔滨整站优化
  • g2g有哪些网站wordpress调用php文件上传
  • 网站建设系统教程网站空间备案流程
  • 建设工程施工证哪个网站查询wordpress heart
  • 百度蜘蛛抓取新网站海外电商平台哪个好
  • 免费 微网站网页设计结构
  • wordpress全站cdn ssl油管代理网页