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

漳州市建设局网站专业自动化网站建设

漳州市建设局网站,专业自动化网站建设,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/24628/

相关文章:

  • 网站建设销售话术开场白网站建设公司市场策划方案
  • 军博网站建设公司网站备案 怎么建站
  • 网站开发要多钱建设网站前端
  • 网站页脚的信息都有什么最专业的网站设计公司有哪些
  • 莱芜做网站创业平台app
  • 个人网站备案所需材料宁波高端定制网站建设
  • 制作企业网站是免费的吗郑州网站推广价格
  • 天津站设计单位宜昌网站优化
  • 做物流的可以在那些网站找客户端外贸网站源码是什么
  • 陕西交通建设集团网站体检网站企业建设公司排名
  • 网站群系统wordpress博客源码
  • php商城建站系统新乐网站建设
  • 全面的聊城网站建设网络公司网站绪论
  • 网站空间付款方式怎么做网站上的销售代
  • 制作网页的网站哪个好用阿里云万网域名
  • 做公众号的模版的网站专业做包包的网站好
  • 企业网站建设方案投标书门户网站建设报价
  • 河南省住房城乡建设厅网站西安市建设和住房保障局网站
  • 彩票网站 在哪里做权威发布图片红字
  • 西安企业网站制作猎头公司网站建设方案
  • 温州制作手机网站服装网站建设图
  • 外汇网站模版大庆市让胡路区规划建设局网站
  • 网站建设微信公众号文章html网站的设计
  • 北京建设网站哪家好域名 不做网站
  • 怎么把网站改为正在建设中网站制作的相关术语
  • 黑龙江网站备案地址常州市建设工程质量监督站网站
  • 看守所加强自身网站建设工作总结个人网站的投稿平台
  • 网站开发流程主要分成什么WordPress开启me
  • 网站标题怎样写公司做网站哪里做
  • 网站影响seo的标签竹子建站是什么