![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
多线程
捕鲸叉
这个作者很懒,什么都没留下…
展开
-
C++ 并发编程练习15 “最简单的线程池”
下面的代码展示了一个简单的线程池。#include <atomic>#include <functional>#include <iostream>#include <mutex>#include <queue>#include <thread>#include <vector>template<typename T>class thread_safe_queue{private:原创 2022-05-02 16:32:59 · 996 阅读 · 0 评论 -
C++并发编程练习14 “异步任务测试”
使用feature特性,可以获得异步执行的效果。如下面的例子#include <iostream> #include <future> #include <chrono> #include <thread> int loopType (int from, int to) { for (int i = from; i != to; ++i) { std::cout <原创 2022-05-02 15:13:10 · 940 阅读 · 0 评论 -
C++并发编程练习13 “线程安全栈设计”
使用mutex锁,设计一个线程安全的stack,示例如下#include <exception>#include <memory>#include <mutex>#include <stack>struct empty_stack : std::exception{ const char* what() const throw();};template<typename T>class threadsafe_sta原创 2022-04-23 17:57:56 · 725 阅读 · 0 评论 -
C++并发编程练习12 “在跨线程中使用函数对象(指针)”
需要谨慎防止被保护的对象意外泄露到外界函数被更改状态。代码示例如下:#include <functional>#include <iostream>#include <mutex>#include <string>#include <thread>struct TData{public: int n; std::string str;public: TData() { n = 0; str = "Mout原创 2022-04-23 16:28:19 · 909 阅读 · 0 评论 -
C++并发编程练习11 “互斥量”
在多线程开发中,常遇到共享数据需要保护的问题,我们可以使用互斥量std::mutext类来解决这个问题,示例如下#include <chrono>#include <iostream>#include <mutex>#include <thread>#include <vector>struct scoped_mutex{ scoped_mutex(std::mutex& mtx) : mt(mt原创 2022-04-23 15:14:13 · 555 阅读 · 0 评论 -
C++并发编程练习10 “线程管理之线程ID”
std::thread有一个get_id()函数可以唯一标识系统中线程。示例如下#include <chrono>#include <iostream>#include <string>#include <thread>#include <vector>std::thread::id master;void func(int i){ std::string msg = std::to_string(i);原创 2022-04-23 00:10:48 · 951 阅读 · 0 评论 -
C++并发编程练习09 “线程管理之线程数目动态确定”
我们可以动态计算并创建合理的线程数目, 代码示例如下#include <algorithm>#include <functional>#include <iostream>#include <numeric>#include <thread>#include <vector>template<typename Iterator, typename T>struct accumulate_block{原创 2022-04-22 23:50:12 · 153 阅读 · 0 评论 -
C++并发编程练习08 “线程管理之线程容器”
我们可以使用容器保存一组线程,如下面的vector装入一组线程:#include <chrono>#include <iostream>#include <string>#include <thread>#include <vector>void func(int n){ std::string str = std::to_string(n); str += '\n'; std::cout <<原创 2022-04-22 22:40:44 · 340 阅读 · 0 评论 -
C++并发编程练习07 “线程管理之线程scoped”
线程scoped在某些场合还是很有用的,可以简化编程,不易出错。示例如下:#include <exception>#include <iostream>#include <thread>struct scoped_thread{ std::thread th; scoped_thread(std::thread&& t) { if (!t.joinable()) throw std::runtime_error("threa原创 2022-04-22 22:19:32 · 240 阅读 · 0 评论 -
C++并发编程练习06 “线程管理之线程对象转移“
线程对象可以和一般对象一样,其所有权可以被转移,示例如下:#include <iostream>#include <thread>void get(std::thread&& t){ t.detach(); int a; std::cin >> a; std::cout << a << std::endl;}void task(const char* sz){ std::cout << s原创 2022-04-22 22:03:47 · 237 阅读 · 0 评论 -
C++并发编程练习05 “线程管理之传递引用参数给任务函数与std::ref绑定”
传递引用时需注意,如下面代码注释中所述:#include <iostream>#include <thread>void task(int& n){ n *= 2;}int main(){ int a = 100; //注意必须用std::ref绑定,否则由于thread构造函数无法识别,它将传入a的副本的引用给task函数 std::thread t(task, std::ref(a)); t.join()原创 2022-04-22 20:31:57 · 269 阅读 · 0 评论 -
C++并发编程练习04 “线程管理之传递参数给任务函数”
我们可以对不同的多线程任务函数传递对应参数。其参考代码如下:#include <iostream>#include <thread>void task(int n, const char* message){ std::cout << n << "," << message << std::endl;}int main(){ std::thread t(task, 1, "something hap原创 2022-04-22 20:17:08 · 285 阅读 · 0 评论 -
C++并发编程练习03 “线程管理之线程守卫”
包装类对象析构时执行线程#include <iostream>#include <thread>struct thread_guard{public: std::thread& t; explicit thread_guard(std::thread& _t) : t(_t) {}; ~thread_guard() { if(t.joinable()) t.join(); } thread_guard(thread_guard&原创 2022-04-22 10:26:31 · 215 阅读 · 0 评论 -
C++并发编程练习02 “线程管理之启动线程”
启动线程有detach()和join()两个函数,前者是异步启动,后者是同步启动并等待结束。#include <iostream>#include <thread>struct task{ int& i; task(int& i_) :i(i_) {}; void operator()() { for (int j = 0; j < 200; j++) std::cout原创 2022-04-22 10:12:38 · 131 阅读 · 0 评论 -
C++并发编程练习01 “Hello World“
#include <iostream>#include <thread>void sayHello(){ std::cout << "Hello World!\n";}int main(){ std::thread t(sayHello); t.join(); return 0;}原创 2022-04-22 10:05:50 · 126 阅读 · 0 评论