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

上海网站seo招聘宁波网站建设系统介绍

上海网站seo招聘,宁波网站建设系统介绍,比特币网站怎么做,36氪 wordpress 主题现代C&#xff08;C11及其之后的版本&#xff09;引入了标准的线程支持库&#xff0c;使得多线程编程变得更加简单和可移植。这个库提供了线程管理、互斥量、条件变量和其他同步原语。 1. std::thread - 基本线程 std::thread允许创建执行特定任务的线程。 #include <ios…

在这里插入图片描述

现代C++(C++11及其之后的版本)引入了标准的线程支持库,使得多线程编程变得更加简单和可移植。这个库提供了线程管理、互斥量、条件变量和其他同步原语。

1. std::thread - 基本线程

std::thread允许创建执行特定任务的线程。

#include <iostream>
#include <thread>void helloFunction() {std::cout << "Hello from thread!" << std::endl;
}void basicThread() {std::thread t(helloFunction);t.join();  // 等待线程完成
}

2. 传递参数给线程函数

线程函数可以接受参数,和普通函数一样。

#include <iostream>
#include <thread>void printMessage(const std::string& message) {std::cout << message << std::endl;
}void threadWithArguments() {std::thread t(printMessage, "Hello from thread with argument!");t.join();
}

3. std::mutex - 互斥量

互斥量用于同步对共享资源的访问。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void printBlocked(const std::string& message) {mtx.lock();std::cout << message << std::endl;mtx.unlock();
}void mutexExample() {std::thread t1(printBlocked, "Thread 1");std::thread t2(printBlocked, "Thread 2");t1.join();t2.join();
}

4. std::lock_guard - 自动管理互斥量

std::lock_guard提供了一种便捷的RAII风格的方式来自动上锁和解锁互斥量。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void safePrint(const std::string& message) {std::lock_guard<std::mutex> lock(mtx);std::cout << message << std::endl;// 互斥量在lock_guard对象被销毁时自动解锁
}void lockGuardExample() {std::thread t1(safePrint, "Thread 1 with lock_guard");std::thread t2(safePrint, "Thread 2 with lock_guard");t1.join();t2.join();
}

5. std::asyncstd::future - 异步执行

std::async允许异步执行函数,并通过std::future获取结果。

#include <iostream>
#include <future>int compute() {return 42;  // 模拟计算
}void asyncExample() {std::future<int> result = std::async(compute);std::cout << "The answer is " << result.get() << std::endl;
}

6. std::condition_variable - 条件变量

条件变量用于线程间的同步,允许线程在特定条件下等待或通知其他线程。

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>std::mutex mtx;
std::condition_variable cv;
bool ready = false;void printId(int id) {std::unique_lock<std::mutex> lock(mtx);while (!ready) cv.wait(lock);std::cout << "Thread " << id << std::endl;
}void go() {std::unique_lock<std::mutex> lock(mtx);ready = true;cv.notify_all();
}void conditionVariableExample() {std::thread threads[10];for (int i = 0; i < 10; ++i) {threads[i] = std::thread(printId, i);}std::cout << "10 threads ready to race..." << std::endl;go();for (auto& t : threads) {t.join();}
}
http://www.yayakq.cn/news/123913/

相关文章:

  • 网站地址栏iis网站搭建
  • 如何查询网站备案进度石家庄网页制作招聘信息
  • 重庆江北营销型网站建设公司哪家好平面设计多久能学会
  • 职高网站建设例题贴吧高级搜索
  • 网站做贸易用什么色调比较好公司网站设计费计入什么科目
  • 网站开发建站教程详解百度百度一下一下
  • 建设论坛网站视频网站代码500
  • 做爰 网站四川建设网项目招标
  • 佛山新网站建设服务wordpress教程 gofair
  • 宿州网站建设公司亚马逊开店流程及费用
  • 公司内部的网站主要作用网站制作公司员工
  • 网站设计画布规范1680自己电脑可以做网站服务器
  • 网站建设方案书内容中国软文网
  • 宁波市环境建设保护局网站外贸网站建设入门
  • 网站建设规划书 简版移动网站 做优化
  • 做网站代码保密协议wordpress在线咨询插件
  • 北京网站建设公司排行榜wordpress集成到app
  • 青海省建设厅网站备案资料目前有哪些网络营销方式
  • 杭州网站定制开发个人备案转企业网站期间
  • 电子商务网站如何建设中铁建设门户网员工登录
  • 网站推广的方式有手机开发安卓软件
  • 软件设计大赛企业网站 更新 seo
  • 用户体验较好的网站php网站开发实用技术答案
  • 青岛建设企业网站最新章节 62.一起来做网站吧
  • 太原网站优化服务网站制作 网站建设 杭州
  • 青岛网站制作百家号福建省建设厅网站电脑板
  • 成都网站建设互联做网站怎么切psd图
  • 传统的网站开发模式wordpress可以注册了
  • 橙网站小米手机做网站服务器
  • 绚丽的网站欣赏西安商城网站建设公司