- 1、原子操作std::atomic续谈
- 2、std::async深入谈
- 2.1 std::async用来创建一个异步任务
- 2.2 std::async和std::thread的区别
- 2.3 std::async不确定性问题的解决
-
一般atomic原子操作,针对++,--,+=,&=,|=,^=是支持的,其他的可能不支持。
-
示例代码:
#include
#include #include using namespace std; //我们封装了一个类型为int的对象(值),具有原子操作性质,我们可以像操作一个int类型变量一样来操作这个g_mycount std::atomic g_mycount = 0; void mythread() //线程入口函数 { for (int i = 0; i < 1000000; i++) { //g_mycount++; //对应的操作室原子操作,不会被打断 //g_mycount += 1; //结果和g_mycount++相同 g_mycount = g_mycount + 1; //结果不对 } return; } int main() { thread mytobj1(mythread); thread mytobj2(mythread); mytobj1.join(); mytobj2.join(); cout << "两个线程执行完毕,最终g_mycount的结果是:" << g_mycount << endl; cout << "I love China!" << endl; return 0; } 结果如下:
结果为错误的,说明sdt::atomic原子操作不支持g_mycount = g_mycount + 1这种形式。
std::futureresult = std::async(mythread);
std::lanuch::deferred
std::futureresult = std::async(std::launch::deferred, mythread);
- std::async()的第一个参数位置,作用:延迟调用,并且不创建新线程,延迟到future对象调用get()或者wait()的时候才执行mythread(),如果没有调用get()或者wait(),mythread()不会执行。
std::lanuch::async
std::futureresult = std::async(std::launch::async, mythread);
- std::async()的第一个参数位置,作用:强制这个异步任务在新线程上执行,这意味着系统必须要给我创建出新线程来执行mythread()。
launch::async | launch::deferred
std::futureresult = std::async(std::launch::deferred | std::launch::async, mythread);
-
Automatic: The function chooses the policy automatically (at some point). This depends on the system and library implementation, which generally optimizes for the current availability of concurrency in the system.
-
自动:该函数自动(在某个点)选择策略。这取决于系统和库实现,通常会针对系统中并发的当前可用性进行优化。
-
意味着调用async的行为可能是“创建新线程并立即执行”或者“没有创建新线程并且延迟到调用get()才开始执行任务入口函数,两者局其一。
不带额外参数,只给async函数一个入口名函数
std::futureresult = std::async(std::launch::deferred | std::launch::async, mythread);
-
会使用默认值,默认值为launch::async | launch::deferred。
-
系统会自行决定是异步(创建新线程)还是同步(不创建新线程)方式运行。
- std::thread():如果系统资源紧张,那么可能std::thread()创建线程就会失败,那么执行std::thread()时整个程序可能崩溃。
- std::async()我们一般不叫创建线程(即使async能够创建线程),我们一般叫它创建一个异步任务。
- std::async()和std::thread()最明显的不同就是,std::async()有时候并不创建新线程。
- std::thread()创建线程的方式,如果有线程返回值,想拿到这个值也不容易。
- std::async()创建异步任务,可能创建线程也可能不创建线程,但std::async()调用方法很容易拿到线程入口函数的返回值。
- 由于系统资源限制:
- 如果用std::thread创建的线程太多,则可能创建失败,系统报告异常,崩溃。
- 如果用std::async,一般就不会报异常或者崩溃,因为,当系统资源紧张导致无法创建新线程的时候,std::async这种不加额外参数的调用就不会创建新线程,而是后续谁调用了get()来请求结果,那么这个异步任务就运行在执行这条get()语句所在的线程上。
- 如果强制使用std::async一定要创建新线程时,那么就必须使用std::lanuch::async。承受的代价就是系统资源紧张时,程序崩溃。
- 经验:一个程序里,线程数量不宜超过100-200。
- 使用std::future::wait_for()函数进行状态判断
示例代码:
#include#include #include using namespace std; int mythread() { cout << "mythread() start " << " threadid = " << std::this_thread::get_id() << endl; std::chrono::milliseconds dura(5000); //5s std::this_thread::sleep_for(dura); return 1; } int main() { cout << "main start " << " threadid = " << std::this_thread::get_id() << endl; std::future result = std::async(mythread); std::future_status status = result.wait_for(0s); //0s:零秒相当于std::chrono::microseconds(0) if (status == std::future_status::deferred) { //任务被延迟了(系统资源紧张,采用了std::lanuch::deferred策略了) cout << result.get() << endl; } else { //任务没有被推迟,已经开始运行了,线程被创建了 if (status == std::future_status::ready) { cout << "线程成功执行完毕并返回!" << endl; cout << result.get() << endl; } else if (status == std::future_status::timeout) { cout << "超时,线程还没执行完,稍等" << endl; cout << result.get() << endl; } } return 0; }
注:本人学习c++多线程视频地址:C++多线程学习地址



