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

江津网站建设公司安顺网站建设兼职

江津网站建设公司,安顺网站建设兼职,网站怎么做需要花钱吗,app图片怎么制作一文讲通锁标记对象std::adopt_lock盲点 1. 核心概念2. 代码详解1. 单个锁2. 多重锁(可以用来预防死锁)3. 条件变量的互斥控制4. 复杂示例: 多生产者-多消费者模型(超纲了, 可不看,哈哈哈哈) 3. 小结 1. 核心概念 在C中, std::adopt_lock是一…

一文讲通锁标记对象std::adopt_lock盲点

    • 1. 核心概念
    • 2. 代码详解
      • 1. 单个锁
      • 2. 多重锁(可以用来预防死锁)
      • 3. 条件变量的互斥控制
      • 4. 复杂示例: 多生产者-多消费者模型(超纲了, 可不看,哈哈哈哈)
    • 3. 小结

1. 核心概念

  在C++中, std::adopt_lock是一个锁标记对象[^1], 用于配合锁对象(如 std::lock_guard、std::unique_lock 或 std::shared_lock)来管理互斥锁(mutex), 它的作用是告诉锁对象:互斥锁已经被手动锁定了,锁对象只需要“接管”这个锁的所有权,而不需要再次尝试锁定。

2. 代码详解

1. 单个锁

#include <iostream>
#include <mutex>
#include <thread>std::mutex mtx;  // Global mutexvoid process() {// Step 1: Manually lock the mutex.mtx.lock();  // The current thread now owns the mutex.// Step 2: Construct a lock_guard to adopt the existing lock.// The std::adopt_lock tag tells lock_guard that mtx is already locked.std::lock_guard<std::mutex> guard(mtx, std::adopt_lock);// Critical section: safe access to shared resources.std::cout << "Inside critical section." << std::endl;// When 'guard' goes out of scope, its destructor will call mtx.unlock().
}int main() {std::thread t(process);t.join();  // Wait for the thread to finish.return 0;
}

  这里的关键点是 std::adopt_lock,它告诉 lock_guard 互斥量已经被锁定,因此 lock_guard 不会尝试再次调用
mtx.lock()

如果没有使用 std::adopt_lock,std::lock_guard 会在构造时试图锁定互斥量,这将导致死锁

2. 多重锁(可以用来预防死锁)

#include <iostream>
#include <mutex>
#include <thread>std::mutex mtx1;
std::mutex mtx2;void task() {// Step 1: Atomically lock both mutexes using std::lock, which prevents deadlock.std::lock(mtx1, mtx2);// Step 2: Create RAII objects that adopt these locks.std::lock_guard<std::mutex> guard1(mtx1, std::adopt_lock);std::lock_guard<std::mutex> guard2(mtx2, std::adopt_lock);// Critical section: safe access to shared resources protected by mtx1 and mtx2.std::cout << "Thread safely acquired both mutexes." << std::endl;// Both mutexes will be unlocked when guard1 and guard2 go out of scope.
}int main() {std::thread t1(task);std::thread t2(task);t1.join();t2.join();return 0;
}

3. 条件变量的互斥控制

  在条件变量中结合 std::unique_lock 和 std::adopt_lock 使用,可以灵活地管理锁的状态。

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>std::mutex mtx;
std::condition_variable cv;
bool ready = false;void workerThread() {// 手动锁定互斥量mtx.lock();std::cout << "Worker thread acquired lock manually.\n";// 使用 adopt_lock 转移锁管理权std::unique_lock<std::mutex> lock(mtx, std::adopt_lock);// 等待条件变量被通知cv.wait(lock, [] { return ready; });std::cout << "Worker thread is processing data.\n";
}void notifierThread() {std::this_thread::sleep_for(std::chrono::seconds(1));// 通知条件变量{std::lock_guard<std::mutex> lock(mtx); // 自动管理锁ready = true;std::cout << "Notifier thread is notifying.\n";}cv.notify_one();
}int main() {std::thread worker(workerThread);std::thread notifier(notifierThread);worker.join();notifier.join();return 0;
}

解释补充

  • worker_thread 手动锁定互斥量并通过 std::adopt_lock 转移锁管理权给 std::unique_lock。
  • notifier_thread 通过 std::lock_guard 安全通知条件变量。

4. 复杂示例: 多生产者-多消费者模型(超纲了, 可不看,哈哈哈哈)

  假设有多个生产者线程向共享队列中添加任务,而多个消费者线程从队列中处理任务。为了避免死锁,使用 std::adopt_lock 配合多个互斥量和条件变量。

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <vector>
#include <chrono>std::mutex queue_mutex;         // 队列的互斥量
std::mutex print_mutex;         // 打印输出的互斥量
std::condition_variable cv;     // 条件变量
std::queue<int> task_queue;     // 共享任务队列
const int MAX_QUEUE_SIZE = 10;  // 队列的最大容量
bool stop = false;              // 用于通知消费者线程停止// 生产者函数
void producer(int id) {for (int i = 0; i < 20; ++i) {std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 模拟生产延迟std::unique_lock<std::mutex> lock(queue_mutex); // 锁定队列的互斥量cv.wait(lock, [] { return task_queue.size() < MAX_QUEUE_SIZE; }); // 等待队列有空间task_queue.push(i);{// 使用 adopt_lock 安全打印日志std::lock_guard<std::mutex> print_lock(print_mutex, std::adopt_lock);std::cout << "Producer " << id << " produced task " << i << ". Queue size: " << task_queue.size() << std::endl;}cv.notify_all(); // 通知消费者}
}// 消费者函数
void consumer(int id) {while (true) {std::unique_lock<std::mutex> lock(queue_mutex);cv.wait(lock, [] { return !task_queue.empty() || stop; }); // 等待队列有任务或停止信号if (stop && task_queue.empty()) break; // 如果停止并且队列为空,退出int task = task_queue.front();task_queue.pop();{// 使用 adopt_lock 安全打印日志std::lock_guard<std::mutex> print_lock(print_mutex, std::adopt_lock);std::cout << "Consumer " << id << " consumed task " << task << ". Queue size: " << task_queue.size() << std::endl;}cv.notify_all(); // 通知生产者}
}int main() {std::vector<std::thread> producers;std::vector<std::thread> consumers;// 启动生产者线程for (int i = 0; i < 3; ++i) {producers.emplace_back(producer, i + 1);}// 启动消费者线程for (int i = 0; i < 2; ++i) {consumers.emplace_back(consumer, i + 1);}// 等待所有生产者完成for (auto& p : producers) {p.join();}// 通知消费者停止{std::lock_guard<std::mutex> lock(queue_mutex);stop = true;}cv.notify_all();// 等待所有消费者完成for (auto& c : consumers) {c.join();}std::cout << "All tasks processed. Exiting program." << std::endl;return 0;
}

3. 小结

std::adopt_lock 适用于以下场景:

  • Lock First, Adopt Later:手动锁定互斥量后,将管理权交给锁管理类(如 std::lock_guard 或 std::unique_lock)。
  • 注释要清晰(就是这么突兀! 没错, 就是没有这个场景!!!, 想想我为什么要写?还标红加粗)
  • 多个互斥量的协调锁定。
  • 配合条件变量灵活管理锁定逻辑。

[^1] 锁标记对象

http://www.dtcms.com/wzjs/589387.html

相关文章:

  • 网站设计网站制作网站建设设计工具
  • 设计公司网站 唐山境外电商网站建设
  • 网站建设 数据归属汽车精品设计网站建设
  • 怎么做非法彩票网站安卓app在线开发
  • 企业网站建设的基本原则有哪些?郑州航空港建设局网站
  • 有哪些可以做问卷的网站北京海淀区有哪些企业
  • 果洛wap网站建设比较好有趣的网站有哪些推荐
  • 深圳制作网站建设的企业网站页面禁止访问
  • 网站推广专家宜昌网站企业
  • 青岛网站公司seo是什么职业
  • 网站建设与管理专业工资高吗建设一个交易网站要用多少钱
  • 网站数据模版手机评测网
  • 可信的昆明网站建设多个网站对比表格怎么做
  • eclipse的开发网站开发wordpress 地理位置签到
  • 网站推广的工具( )广西网站建设工具
  • 先备案还是先做网站网站见建设
  • 做动态二维码的网站沈阳祥云男科
  • 邢台开发区网站网站建设客户来源
  • php制作网站seo快速排名服务
  • 网站设计的能力要求安福县住房和城乡建设局网站
  • 我买了一个备案网站 可是公司注销了搜索引擎营销方案例子
  • 客户网站加一个功能 应该怎么做女性广告
  • 合肥建设集团招聘信息网站深圳高端包装盒设计
  • 高端定制网站设计公司o2o网站建设效果
  • 垂直网站做益智类问答商城系统网站模板
  • 制作网站时搜索图标如何做南山制作网站
  • 运城 网站 建设 招聘电子商城网站开发公司
  • 腾冲市住房和城乡建设局网站网易企业邮箱是什么
  • 网站开发客户个人网站页面
  • 鞍山网站wordpress print_r