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

有哪些做室内设计好用的网站网站页面

有哪些做室内设计好用的网站,网站页面,装修网站怎样做,社团的工商年检网站在哪里做参考资料&#xff1a; 2、5.lock_guard 与 std::unique_lock-陈子青的编程学习课堂 (seestudy.cn) 3、C11 多线程编程-小白零基础到手撕线程池_哔哩哔哩_bilibili 一、 C11 Thead线程库的基本使用 # include <thread> std::thread t(function_name, args...); // 线…

参考资料:

2、5.lock_guard 与 std::unique_lock-陈子青的编程学习课堂 (seestudy.cn)

3、C++11 多线程编程-小白零基础到手撕线程池_哔哩哔哩_bilibili

一、 C++11 Thead线程库的基本使用

# include <thread>
std::thread t(function_name, args...);   // 线程开始运行
t.join() // 等待线程完成
t.detach() // 分离线程,让它在后台运行

示例代码一

#include <iostream>
#include <thread>
void print_message() {    std::cout << "Hello, world!" << std::endl;
}
int main() {    std::thread t(print_message);t.join();    return 0;
}

示例代码二

#include <iostream>
#include <thread>
#include <functional> // 包含 std::ref
using namespace std;void increment(int& x) {x++; // 对参数进行递增操作
}int main() {int num = 5;thread t(std::ref(increment), std::ref(num));  // 可用方式一// thread t(increment, std::ref(num));  // 可用方式二// thread t(increment, num); 这个会报错!!!t.join();cout << "After increment: " << num << endl;cout << "increment: " << increment << endl;return 0;
}

二、 C++11 Thead易错

易错一:多线程使用了局部变量

1、有些运行环境下会出现Aborted (core dumped),原因是a是局部变量,在test作用域内会消失,而在线程内继续引用了a;

2、有些运行环境是不会报错,但是会出现结果不可预测

hq@nuc:~/java/my-project2$ ./a.out 
32766

示例代码

#include <iostream>
#include <thread>
std::thread t;
// int a = 1;  // 正确示例应该将a变成全局区域
void foo(int& x) {std::this_thread::sleep_for(std::chrono::seconds(2));x += 1;std::cout << x << std::endl;
}void test(){int a = 1;t = std::thread(foo, std::ref(a));
}int main() {test();t.join();return 0;
}return 0;
}

易错二:多线程使用了局部变量

1、有些运行环境下会出现Aborted (core dumped),原因是a是局部变量,在test作用域内会消失,而在线程内继续引用了a;

2、有些运行环境是不会报错,但是会出现结果不可预测

(py37) hq@nuc:~/java/my-project2$ ./a.out 
0
1

示例代码二:

#include <iostream>
#include <thread>
std::thread t;
void foo(int* x) {std::this_thread::sleep_for(std::chrono::seconds(2));std::cout << *x << std::endl;*x += 1;std::cout << *x << std::endl;
}int main() {int *a = new int(20);t = std::thread(foo, a);delete a;t.join();return 0;
}

正确示例

#include <iostream>
#include <thread>
// std::thread t;
void foo(int* x) {std::this_thread::sleep_for(std::chrono::seconds(2));std::cout << *x << std::endl;*x += 1;std::cout << *x << std::endl;
}int main() {std::shared_ptr<int> sharedPtr = std::make_shared<int>(42);std::thread t([sharedPtr]() {foo(sharedPtr.get());});std::thread t(foo, sharedPtr.get());t.join();return 0;
}

线程池

#include <iostream> // 包含标准输入输出流的头文件。
#include <thread> // 包含线程相关的头文件。
#include <vector> // 包含向量容器的头文件。
#include <queue> // 包含队列容器的头文件。
#include <mutex> // 包含互斥量的头文件,用于实现线程安全。
#include <condition_variable> // 包含条件变量的头文件,用于实现线程同步。
#include <functional> // 包含函数对象的头文件,用于传递任务函数。
#include <glog/logging.h>class ThreadPool { // 定义了一个名为 ThreadPool 的类。
public:ThreadPool(size_t numThreads) : stop(false) { // 线程池的构造函数,接受一个参数 numThreads,表示线程池中的线程数量。初始化列表 stop(false) 初始化了成员变量 stop,将其设置为 false,表示线程池初始状态下不处于停止状态。for (size_t i = 0; i < numThreads; ++i) { // 使用循环创建指定数量的工作线程。workers.emplace_back( // 在工作线程向量中添加一个新的线程,使用 Lambda 表达式初始化线程的执行函数。[this] {while (true) { // 工作线程的主循环,保持线程池始终处于运行状态。std::function<void()> task; // 定义了一个函数对象 task,用于存储要执行的任务。{std::unique_lock<std::mutex> lock(queueMutex); // 创建一个互斥锁 lock,用于保护任务队列。condition.wait(lock, [this] { return stop || !tasks.empty(); }); // 等待条件变量,直到满足 stop 或者任务队列不为空的条件。if (stop && tasks.empty()) { return; } // 如果线程池被要求停止并且任务队列为空,则退出线程。task = std::move(tasks.front()); // 从任务队列中获取任务并移动到 task 中。tasks.pop(); // 从任务队列中移除任务。}task(); // 执行任务。}});}}template<class F>void enqueue(F&& f) { // 定义一个模板函数 enqueue,用于向任务队列中添加任务。{std::unique_lock<std::mutex> lock(queueMutex); // 创建一个互斥锁 lock,用于保护任务队列。tasks.emplace(std::forward<F>(f)); // 将任务添加到任务队列中。}condition.notify_one(); // 通知一个等待中的线程有新任务可执行。}~ThreadPool() { // 线程池的析构函数,用于停止线程池并等待所有线程完成工作。{std::unique_lock<std::mutex> lock(queueMutex); // 创建一个互斥锁 lock,用于保护任务队列。stop = true; // 将停止标志设置为 true,表示线程池将要停止。}condition.notify_all(); // 通知所有等待中的线程停止。for (std::thread& worker : workers) { worker.join(); } // 等待所有工作线程完成工作并退出。}private:std::vector<std::thread> workers; // 存储工作线程的向量。std::queue<std::function<void()>> tasks; // 存储任务的队列,每个任务都是一个可调用的函数对象。std::mutex queueMutex; // 保护任务队列的互斥量。std::condition_variable condition; // 用于线程同步的条件变量。bool stop; // 表示线程池是否停止的标志。
};// 示例任务函数
void taskFunction(int taskId) {// std::cout << "Task " << taskId << " is running in thread " << std::this_thread::get_id() << std::endl;// std::cout << "Task " << taskId << std::endl;LOG(INFO) << "Task " << taskId << " is running in thread " << std::this_thread::get_id() << std::endl;std::this_thread::sleep_for(std::chrono::seconds(1));
}int main() {ThreadPool pool(10); // 创建一个拥有4个线程的线程池// 将一些任务提交到线程池for (int i = 0; i < 1000; ++i) {pool.enqueue([i] { taskFunction(i); });}// 主线程等待所有任务完成std::this_thread::sleep_for(std::chrono::seconds(2)); // 等待足够的时间以确保所有任务完成return 0;
}

线程数组、任务数组

1、线程数组,是不能关闭的,只能是while(true)

2、线程数组,循环从任务数组中取任务

http://www.hkea.cn/news/66187/

相关文章:

  • wordpress在线pdfseo百度站长工具查询
  • 太仓网站建设有限公司网站设计公司怎么样
  • 网站去哪做在线crm软件
  • 做360手机网站快速汕头seo排名收费
  • 网站建设总做总结宜兴百度推广公司
  • 做毕业网站的周记外贸建站优化
  • 南昌市住房和城乡建设局网站百度官网推广平台电话
  • 真人做视频网站百度怎么发布广告
  • 网站页面优化包括怎么给网站做优化
  • 哪个网站用帝国cms做的软文素材网
  • 网站建设需要的资料深圳精准网络营销推广
  • 客户网站建设公司网站排名提升软件
  • 网站建设与维护试卷论文怎么在百度上做广告
  • 做博客网站要什么技术百度网站网址是多少
  • 河北建设厅官方网站八大员考试站长工具查询
  • 大连 做网站公司爱站工具包的主要功能
  • ps做简洁大气网站必应bing国内版
  • 做公司标志用哪个网站营销自动化
  • wordpress5.0.3厦门百度seo
  • 网站开发 企业 定制系统优化大师安卓版
  • 网站内链符号seo百度站长工具
  • 网站页面太多是否做静态seo优化软件
  • mac下怎么安装wordpress关键词排名优化易下拉霸屏
  • 国内做国外代购在哪个网站好百度平台客服怎么联系
  • 菏泽网站获客网站建设公司中国站长网入口
  • 黄冈网站建设推荐seo查询排名软件
  • 自己怎么做百度网站广州seo网站公司
  • 京东企业的电子网站建设百度seo教程网
  • 弥勒网站设计公司share群组链接分享
  • 网站建设栏目管理百度推广搜索排名