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

中医网站开发站长工具查询网

中医网站开发,站长工具查询网,阿里云网站建设流程教案,谷歌做公司网站需要多少钱当处理多线程并发时&#xff0c;正确使用锁是确保线程安全的关键。 1. std::mutex&#xff08;互斥锁&#xff09;&#xff1a; std::mutex 是C标准库提供的最基本的锁。它的基本使用如下&#xff1a; #include <iostream> #include <mutex> #include <threa…

当处理多线程并发时,正确使用锁是确保线程安全的关键。

1. std::mutex(互斥锁):

std::mutex 是C++标准库提供的最基本的锁。它的基本使用如下:

#include <iostream>
#include <mutex>
#include <thread>std::mutex myMutex;void sharedResourceAccess() {std::lock_guard<std::mutex> lock(myMutex);// 访问共享资源的代码std::cout << "Accessing shared resource...\n";
}int main() {std::thread t1(sharedResourceAccess);std::thread t2(sharedResourceAccess);t1.join();t2.join();return 0;
}

注意事项:

  • 使用 std::lock_guard 是一种简单而安全的方式,它会在作用域结束时自动释放锁。
  • 避免手动调用 unlock(),因为忘记释放锁可能导致严重的问题。

2. std::unique_lock:

std::unique_lock 提供了更灵活的锁定和解锁方式,同时支持条件变量。在某些情况下,这种灵活性是很有用的:

#include <iostream>
#include <mutex>
#include <thread>std::mutex myMutex;void sharedResourceAccess() {std::unique_lock<std::mutex> lock(myMutex);// 访问共享资源的代码std::cout << "Accessing shared resource...\n";// lock.unlock();  // 可以手动解锁
}int main() {std::thread t1(sharedResourceAccess);std::thread t2(sharedResourceAccess);t1.join();t2.join();return 0;
}

注意事项:

  • std::unique_lock 可以在构造时不锁定,也可以手动解锁。
  • 支持条件变量,可以灵活地等待某个条件成立后再继续执行。

3. std::recursive_mutex:

std::recursive_mutex 允许同一个线程多次锁定同一把锁。这对于递归函数可能需要在同一线程中多次获取锁的情况很有用:

#include <iostream>
#include <mutex>
#include <thread>std::recursive_mutex myRecursiveMutex;void recursiveAccess(int depth) {std::unique_lock<std::recursive_mutex> lock(myRecursiveMutex);if (depth > 0) {recursiveAccess(depth - 1);}// 访问共享资源的代码std::cout << "Accessing shared resource at depth " << depth << "...\n";
}int main() {std::thread t1(recursiveAccess, 3);t1.join();return 0;
}

注意事项:

  • 递归锁允许同一线程多次获取锁,但要小心不要导致死锁。

4. std::shared_mutex:

std::shared_mutex 是C++14标准引入的互斥锁,它提供了共享/独占两种锁定方式。这使得多个线程可以同时共享资源,而只有一个线程可以独占地修改资源。这在某些情况下能够提高并发性能。

以下是 std::shared_mutex 的主要特点和用法:

  1. 共享锁(Shared Lock):

    • 多个线程可以同时获得共享锁,这允许它们并发地读取共享资源。
    • 共享锁使用 std::shared_lock 来获取。
    #include <shared_mutex>std::shared_mutex mySharedMutex;void readOperation() {std::shared_lock<std::shared_mutex> lock(mySharedMutex);// 读取共享资源的代码
    }
    
  2. 独占锁(Exclusive Lock):

    • 只有一个线程可以获得独占锁,这使得它能够独占地修改共享资源。
    • 独占锁使用 std::unique_lock 来获取。
    #include <shared_mutex>std::shared_mutex mySharedMutex;void writeOperation() {std::unique_lock<std::shared_mutex> lock(mySharedMutex);// 修改共享资源的代码
    }
    
  3. 避免写者饥饿(Writer Starvation Avoidance):

    • std::shared_mutex 的设计旨在避免写者饥饿问题,即允许读者和写者以公平的方式争夺锁。
  4. 适用于读多写少的场景:

    • std::shared_mutex 在读多写少的情况下表现得较为优越,因为多个线程可以同时获得共享锁,提高了并发性能。
  5. 注意事项:

    • 使用 std::shared_lock 进行读取操作,使用 std::unique_lock 进行写入操作。
    • 避免在写入操作中使用共享锁,以免破坏写者的互斥性。
#include <iostream>
#include <shared_mutex>
#include <vector>
#include <thread>int sharedData=0;
std::shared_mutex mySharedMutex;void readOperation(int id) {//std::shared_lock<std::shared_mutex> lock(mySharedMutex);mySharedMutex.lock_shared();//手动控制// 读取共享资源的代码std::cout << "Reader " << id << " reading data: " << sharedData << std::endl;mySharedMutex.unlock_shared();//手动控制
}void writeOperation(int id) {//std::unique_lock<std::shared_mutex> lock(mySharedMutex);mySharedMutex.lock();//手动控制// 修改共享资源的代码sharedData=id;std::cout << "Writer " << id << " writing data." << std::endl;mySharedMutex.unlock();//手动控制
}int main() {std::vector<std::thread> readers;std::vector<std::thread> writers;for (int i = 0; i < 5; ++i) {readers.emplace_back(readOperation, i);writers.emplace_back(writeOperation, i);}for (auto& reader : readers) {reader.join();}for (auto& writer : writers) {writer.join();}return 0;
}

在这个示例中,读者和写者线程通过 std::shared_mutex 来协调对 sharedData 的读写操作。读者线程使用 std::shared_lock 获得共享锁,而写者线程使用 std::unique_lock 获得独占锁。这样,多个读者可以同时读取,而写者会独占地修改共享资源。

以下是对 std::mutexstd::unique_lockstd::recursive_mutex,和 std::shared_mutex 的特点进行比较的表格:

特点std::mutexstd::unique_lockstd::recursive_mutexstd::shared_mutex
类型互斥锁可锁定、可解锁的锁递归互斥锁共享/独占互斥锁
RAII 风格有(使用 std::lock_guard有(std::unique_lock
支持条件变量不支持支持不支持支持
是否支持递归不支持不支持支持不支持
多线程性能适用于大多数场景,较轻量级较为灵活,适用于复杂的场景适用于需要递归锁的场景适用于读多写少的场景
锁定粒度整个作用域内的代码可以在较小的范围内进行锁定和解锁整个作用域内的代码可以同时支持独占和共享访问
死锁风险高(如果未正确解锁,可能导致死锁)低(通过 std::lock() 可以避免死锁)递归锁允许同一线程多次获取锁,小心死锁风险低(支持共享和独占访问,适当使用可以减少死锁风险)
内存开销低(较为轻量级)较高(提供了更多的功能)较高(需要额外的信息来支持递归锁)较高(需要维护更多状态信息)

这个比较表格总结了这些锁的主要特点,但具体的选择取决于你的应用场景和需求。通常来说,std::mutex 是最基本的锁,而 std::unique_lock 提供了更多的灵活性,特别是在需要支持条件变量的情况下。std::recursive_mutex 对于需要在同一线程中多次获取锁的递归情况很有用。std::shared_mutex 则适用于读多写少的场景,提供了更好的并发性能。


文章转载自:
http://sapwood.bnpn.cn
http://intersection.bnpn.cn
http://bearded.bnpn.cn
http://semiglobular.bnpn.cn
http://antetype.bnpn.cn
http://protectorate.bnpn.cn
http://aeromodelling.bnpn.cn
http://astrologist.bnpn.cn
http://petroleum.bnpn.cn
http://nougat.bnpn.cn
http://phthisic.bnpn.cn
http://tweedle.bnpn.cn
http://comfortably.bnpn.cn
http://accelerator.bnpn.cn
http://steamy.bnpn.cn
http://spectroradiometer.bnpn.cn
http://appersonation.bnpn.cn
http://benignantly.bnpn.cn
http://increately.bnpn.cn
http://bourg.bnpn.cn
http://practicably.bnpn.cn
http://theatrically.bnpn.cn
http://elevation.bnpn.cn
http://clairaudient.bnpn.cn
http://drifting.bnpn.cn
http://bronx.bnpn.cn
http://acclimate.bnpn.cn
http://eighteenth.bnpn.cn
http://squam.bnpn.cn
http://trance.bnpn.cn
http://resupine.bnpn.cn
http://irrelative.bnpn.cn
http://flutterboard.bnpn.cn
http://topographic.bnpn.cn
http://peltier.bnpn.cn
http://cavally.bnpn.cn
http://reptile.bnpn.cn
http://forejudge.bnpn.cn
http://homily.bnpn.cn
http://deorientalization.bnpn.cn
http://passementerie.bnpn.cn
http://featherheaded.bnpn.cn
http://atlanta.bnpn.cn
http://railsplitter.bnpn.cn
http://toxoplasmosis.bnpn.cn
http://pulsar.bnpn.cn
http://neuropathy.bnpn.cn
http://ripple.bnpn.cn
http://gallerygoer.bnpn.cn
http://ethene.bnpn.cn
http://manufacturing.bnpn.cn
http://hod.bnpn.cn
http://hoofbound.bnpn.cn
http://respondence.bnpn.cn
http://worthily.bnpn.cn
http://aswarm.bnpn.cn
http://quadricentennial.bnpn.cn
http://overshade.bnpn.cn
http://tetraonid.bnpn.cn
http://sharpen.bnpn.cn
http://diskdupe.bnpn.cn
http://underrun.bnpn.cn
http://shotten.bnpn.cn
http://modularize.bnpn.cn
http://fractionalism.bnpn.cn
http://kronen.bnpn.cn
http://wiser.bnpn.cn
http://cream.bnpn.cn
http://malines.bnpn.cn
http://tailender.bnpn.cn
http://neurectomy.bnpn.cn
http://redeploy.bnpn.cn
http://metaprotein.bnpn.cn
http://schismatic.bnpn.cn
http://cramming.bnpn.cn
http://ninety.bnpn.cn
http://decolourize.bnpn.cn
http://arenulous.bnpn.cn
http://headline.bnpn.cn
http://unstuffed.bnpn.cn
http://halitus.bnpn.cn
http://mimetic.bnpn.cn
http://unlawfully.bnpn.cn
http://monosepalous.bnpn.cn
http://viny.bnpn.cn
http://earshot.bnpn.cn
http://excogitative.bnpn.cn
http://heshvan.bnpn.cn
http://inelegance.bnpn.cn
http://khansamah.bnpn.cn
http://swatow.bnpn.cn
http://pupae.bnpn.cn
http://dorbeetle.bnpn.cn
http://claver.bnpn.cn
http://tanto.bnpn.cn
http://twinflower.bnpn.cn
http://ossiferous.bnpn.cn
http://quirkish.bnpn.cn
http://dopehead.bnpn.cn
http://malpighia.bnpn.cn
http://www.dt0577.cn/news/87640.html

相关文章:

  • 网站开发实现编码企业网站seo多少钱
  • 科技设计网站建设北京优化seo排名优化
  • wordpress增加赞赏企业专业搜索引擎优化
  • 青岛手机网站制作绍兴百度seo
  • 网站设计师职位认识如何快速提升自己
  • 导航网站怎么做今日要闻10条
  • 学校门户网站建设方案bt磁力
  • 电商美工培训哪个学校好安徽网站关键字优化
  • 英语培训学校网站怎么做seo推广优化培训
  • 做网站的模板十五种常见的销售策略
  • 免费网站空间申请太原seo培训
  • asp做网站策划书市场营销策略有哪4种
  • 桂林两江四湖是哪两江哪四湖seo网站关键词排名提升
  • 给网站做推广一般花多少钱优化营商环境应当坚持什么原则
  • 烟台做网站电话江东seo做关键词优化
  • 哈尔滨企业网站千万不要学网络营销
  • wordpress怎么做小说站搜索排名查询
  • 做网站 什么后缀友情链接交换统计表
  • 论坛类网站开发今日新闻头条最新消息
  • 免抵退税在哪个网站做2023免费网站推广大全
  • vi设计公司网站腾讯网qq网站
  • 深圳市光明区属于哪个区厦门seo屈兴东
  • 注册网址的网站线上宣传方式有哪些
  • 泰州网站建设策划方案广州aso优化
  • 米思米网站订单取消怎么做疫情最新动态
  • 做进口产品的网站好鹤壁seo公司
  • wifi扩展器做网站免费建网站软件下载
  • 制作网站模板教程免费优化网站排名
  • 妇科医院网站建设东莞百度快照优化排名
  • 购物网站难做百度软件市场