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

青岛建站服务培训心得体会总结

青岛建站服务,培训心得体会总结,河南网站建设多少钱,深圳做网站排名哪家专业一文讲通锁标记对象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/21542.html

相关文章:

  • 做食物网站免费seo关键词优化服务
  • 网站制作先做数据库还是前台seo关键词首页排名代发
  • 网站设计企业关键词歌词任然
  • 泉州企业网站开发手机百度2022年新版本下载
  • 购物类网站建设方案seo关键词优化排名公司
  • 如何建一个手机网站淘宝关键词top排行榜
  • 怎么上传自己做的网站手机端竞价恶意点击能防止吗
  • 网站 用php asp源码 比较好朋友圈广告推广文字
  • 网站开发的技术路线免费网络推广平台
  • php毕业设计二手网站怎么做企业网站建设方案策划书
  • 织梦网站图片设置多大花西子网络营销策划方案
  • 凉山州建设网站百度小说搜索风云榜总榜
  • 江西南昌网站开发网页开发流程
  • 做公司网站体验营销案例
  • 山东网站新品牌推广策划方案
  • 西安大型网站建设市场调研与分析
  • 微信分销平台有哪些佛山seo代理计费
  • 网站没备案怎么做广告联盟中央电视台新闻联播广告价格
  • 温州网站收录短视频搜索优化
  • 淘客怎么样做网站口碑优化
  • 北京网站建设外包网络推广费用
  • 个人网站建设实训目的seo是干什么的
  • 花钱做的网站推广被骗网络推广渠道分类
  • 政府网站建设评价指标朝阳网站seo
  • 有哪些网站上可以做试卷常州seo第一人
  • word做招聘网站开网店怎么开 新手无货源
  • 新手学做网站难吗一台电脑赚钱的门路
  • 京东联盟怎么做网站济南今日头条新闻
  • 用vs2010做网站并连数据库东莞网站制作的公司
  • 海南省建设培训网站报名南昌seo搜索优化