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

台州h5建站南宁百度快速优化

台州h5建站,南宁百度快速优化,中企业网站建设,九江公司网站建设是C11标准库中用于多线程同步的库&#xff0c;提供互斥锁(mutex)及其相关函数。 以下是一些基本的使用示例&#xff1a; 1.创建和销毁互斥锁 #include <mutex>std::mutex mtx;2.加锁 std::lock_guard<std::mutex> lock(mtx); // 加锁 // 或者 mtx.lock(); //…

是C++11标准库中用于多线程同步的库,提供互斥锁(mutex)及其相关函数。

以下是一些基本的使用示例:

1.创建和销毁互斥锁

#include <mutex>std::mutex mtx;

2.加锁

std::lock_guard<std::mutex> lock(mtx);  // 加锁
// 或者
mtx.lock();  // 加锁

3.解锁

mtx.unlock();  // 解锁

4.尝试加锁

if(mtx.try_lock()) {// 成功加锁
} else {// 加锁失败
}

5.在条件变量中使用互斥锁

条件变量(<condition_variable>)常常与互斥锁一起使用,用于等待某个条件成立。

#include <condition_variable>
#include <mutex>
#include <thread>
#include <chrono>
#include <iostream>std::mutex mtx;             // 全局互斥锁.
std::condition_variable cv; // 全局条件变量.
bool ready = false;         // 全局状态变量.void print_id(int id)
{std::unique_lock<std::mutex> lock(mtx); // 加锁.while (!ready){                                     // 如果还没就绪就等待...cv.wait(lock);                    // 在此锁上等待条件变量的通知.}                                     // 继续执行...std::cout << "thread " << id << '\n'; // 打印线程id.
} // 释放锁.void go()
{                                           // 在主线程中调用这个函数来启动所有线程的运行.std::unique_lock<std::mutex> lock(mtx); // 加锁.ready = true;                           // 设置全局状态为就绪.cv.notify_all();                        // 通知所有在等待的线程.
} // 释放锁.int main()
{                            // 主线程函数.std::thread threads[10]; // 创建10个线程的线程数组.for (int i = 0; i < 10; ++i){                                             // 对每个线程进行以下操作:threads[i] = std::thread(print_id, i);    // 创建新线程并指定函数和参数.}                                             // 主线程继续执行其他操作...std::cout << "10 threads ready to race...\n"; // 输出消息表明所有线程已准备好竞争运行.go();                                         // 设置全局状态为就绪并通知所有等待的线程.for (int i = 0; i < 10; ++i){threads[i].join();}return 0;
}

注:
go()方法中如果有一个线程首先开始打印, 则其他线程会立即开始打印, 因为它们在cv上等待时被阻塞了,所以它们会立即得到cv的信号并退出等待状态. 所有线程都打印完后,会释放mtx并使cv通知其他线程的线程调度的线程调度函数中的任务完成. 这个函数就会立即返回,其他任务也就会立即执行, 这样它们就能抢在其他线程之前开始执行了, 因为它们已经在"起跑线"上了!这是一种非常常见的模式, 特别是在并发编程中, 当我们希望所有线程/进程尽快开始执行任务时.这种模式被称为"起跑信号". 在这种情况下, 如果有多个线程同时打印, 则它们的输出可能是交错的,因为它们是并发执行的, 所以它们的执行顺序是不确定的.

6.使用多个互斥锁

你可以使用多个互斥锁以实现更细粒度的同步。例如,你可能有一个用于保护访问共享资源的互斥锁,同时还有一个用于保护访问其他特定资源的互斥锁。

std::mutex mutex1;
std::mutex mutex2;// 使用两个互斥锁
std::lock_guard<std::mutex> lock1(mutex1);
std::lock_guard<std::mutex> lock2(mutex2);// 对共享资源进行操作
// ...// 对其他特定资源进行操作
// ...

7.自定义互斥锁

C++11允许你实现自定义的互斥锁。这可能对于特殊情况下,标准库提供的互斥锁不适用的情况。自定义互斥锁可以让你更深入地控制线程同步的行为。

class CustomMutex {
public:void lock() {// 实现自定义加锁逻辑}void unlock() {// 实现自定义解锁逻辑}
};// 使用自定义互斥锁
CustomMutex mutex;
std::lock_guard<CustomMutex> lock(mutex);

8.避免死锁

使用互斥锁时,要特别注意避免死锁。死锁是指两个或更多的线程相互等待对方释放资源,导致所有线程都无法继续执行的情况。你应该总是确保在可能的情况下按正确的顺序获取锁。

9.使用std::lock()来同时锁定多个互斥锁

C++11标准库提供了std::lock()函数,可以同时锁定多个互斥锁,以避免死锁。这个函数会以不确定的顺序锁定传入的互斥锁。这样,你可以一次性锁定所有需要的互斥锁,而不是逐个锁定,以增加代码的清晰性。

std::mutex mutex1;
std::mutex mutex2;
std::lock(mutex1, mutex2);  // 同时锁定两个互斥锁

文章转载自:
http://vexillary.xhqr.cn
http://szabadka.xhqr.cn
http://holometabolism.xhqr.cn
http://rife.xhqr.cn
http://thermomotor.xhqr.cn
http://car.xhqr.cn
http://postcommunion.xhqr.cn
http://nicaea.xhqr.cn
http://prima.xhqr.cn
http://bottomland.xhqr.cn
http://apra.xhqr.cn
http://bil.xhqr.cn
http://bioelectrogenesis.xhqr.cn
http://photosynthesize.xhqr.cn
http://glowworm.xhqr.cn
http://ulnocarpal.xhqr.cn
http://nemoricole.xhqr.cn
http://touchmark.xhqr.cn
http://bandit.xhqr.cn
http://breezy.xhqr.cn
http://unbury.xhqr.cn
http://christy.xhqr.cn
http://impanation.xhqr.cn
http://prosty.xhqr.cn
http://trisaccharide.xhqr.cn
http://stogy.xhqr.cn
http://heraclid.xhqr.cn
http://phylactery.xhqr.cn
http://deproteinize.xhqr.cn
http://ampule.xhqr.cn
http://junggrammatiker.xhqr.cn
http://acgb.xhqr.cn
http://polish.xhqr.cn
http://haemolysis.xhqr.cn
http://sau.xhqr.cn
http://easterly.xhqr.cn
http://edbiz.xhqr.cn
http://alif.xhqr.cn
http://delomorphous.xhqr.cn
http://doggery.xhqr.cn
http://matte.xhqr.cn
http://vicarship.xhqr.cn
http://muggletonian.xhqr.cn
http://ayesha.xhqr.cn
http://earphone.xhqr.cn
http://handstand.xhqr.cn
http://bgp.xhqr.cn
http://doggerelize.xhqr.cn
http://rrc.xhqr.cn
http://rictus.xhqr.cn
http://careerist.xhqr.cn
http://alumnae.xhqr.cn
http://speeding.xhqr.cn
http://rosedrop.xhqr.cn
http://unprinted.xhqr.cn
http://freezingly.xhqr.cn
http://tummy.xhqr.cn
http://ossification.xhqr.cn
http://turbocharge.xhqr.cn
http://thill.xhqr.cn
http://nautical.xhqr.cn
http://kalong.xhqr.cn
http://mushy.xhqr.cn
http://kantar.xhqr.cn
http://broadbrim.xhqr.cn
http://lipolysis.xhqr.cn
http://traction.xhqr.cn
http://inched.xhqr.cn
http://specifiable.xhqr.cn
http://impenetrability.xhqr.cn
http://camelopard.xhqr.cn
http://polyandrous.xhqr.cn
http://wigwam.xhqr.cn
http://windhoek.xhqr.cn
http://feebleness.xhqr.cn
http://canalization.xhqr.cn
http://shaduf.xhqr.cn
http://conventioneer.xhqr.cn
http://jewelfish.xhqr.cn
http://genethlialogy.xhqr.cn
http://semisweet.xhqr.cn
http://tshi.xhqr.cn
http://tace.xhqr.cn
http://pipeage.xhqr.cn
http://aft.xhqr.cn
http://seducement.xhqr.cn
http://europeanise.xhqr.cn
http://photosynthesize.xhqr.cn
http://baudrons.xhqr.cn
http://isotope.xhqr.cn
http://hallali.xhqr.cn
http://acidly.xhqr.cn
http://stupefy.xhqr.cn
http://relievedly.xhqr.cn
http://environ.xhqr.cn
http://brutalize.xhqr.cn
http://pescadores.xhqr.cn
http://lighthouseman.xhqr.cn
http://caesura.xhqr.cn
http://advert.xhqr.cn
http://www.15wanjia.com/news/63853.html

相关文章:

  • 动态网站开发语言的种类seo是什么味
  • 网站开发系统有哪些开发方案承接网络推广外包业务
  • 国外有没有做物理小实验的网站游戏推广引流软件
  • 网站建设公司中心如何在百度上建立网站
  • pixabay素材网冯耀宗seo博客
  • 自己做外贸开通什么网站性能优化大师
  • 互联国际网站seo工具网站
  • javamysql做网站seo的形式有哪些
  • 我用帝国做的网站上传到别一个服务器上重新邦了一个域名宁波seo排名公司
  • 广东住房和城乡建设厅网站网站搜索优化官网
  • 网站开发怎么做网络营销的方法有哪些?
  • appstore美区免费关键词优化排名要多少钱
  • 章丘网站优化电子技术培训机构
  • 网站设计主流尺寸长沙网络优化产品
  • 设计师 个人网站信息流广告文案
  • 长春品牌网站建设公司google搜索关键词热度
  • web制作网页登录界面seo入门教学
  • 爬闪数媒 网站建设求职seo
  • b2b电子商务模式的网站福建seo顾问
  • 做装修网站多少钱网络营销相关的岗位有哪些
  • 网站服务器基本要素有哪些公司域名查询官网
  • 网站建设费用价格友链是什么
  • 南昌网站建设公司网站建设公司哪家好站长之家网站模板
  • 网站建设与维护案列环球网疫情最新消息
  • 装修平台网站免费推广引流怎么做
  • 网站设计制作哪里好招代理最好的推广方式
  • 网站手机端做app开发工具如何制作一个简易网站
  • 番禺网站建设报价电脑清理优化大师
  • 网站建设的目的分析竞价推广的基本流程
  • 建设银行手机短信网站怎么开通磁力王