C++多线程编程第十一讲--std::atomic续谈、std::async深入谈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | //(1)原子操作std::atomic续谈 // 原子操作针对++,--,+=,-=,&=,|=,~=是支持的,其他的可能不支持。 #include<iostream> #include<mutex> #include<thread> #include<future> using namespace std; //int g_mycount = 0; std::atomic< int > g_mycount = 0; //具备原子操作的整型值 void my_thread() { int i = 0; for (; i < 1000000; ++i) { g_mycount++; //g_mycount += 1; 正确 //g_mycount = g_mycount + 1; //结果错误 } } int main() { cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl; thread mythread(my_thread); thread mythread2(my_thread); mythread.join(); mythread2.join(); cout << g_mycount << endl; cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl; return 0; } //(2)std::async深入谈 // std::async和thread最大的不同就是std::async可能不创建新的线程,具体的执行动作可以由参数决定。 //(2.1) std::async参数详解 // std::launch::deferred 延迟调用,不会创建一个新的线程 // std::launch::async 立即创建一个线程 // 如果什么也不写,默认值是std::launch::async | std::launch::deferred // 系统会根据实际情况选择其中之一来执行。系统资源紧缺时,会使用std::launch::deferred,因为不需要创建新的线程; // 如果系统资源宽松时,就会选择std::launch::async,创建一个新的线程来执行代码。 //(2.2) std::async和std::thread的区别 // 在资源紧张时,使用thread来创建线程,如果发生错误,程序会直接崩溃。 // 这个时候如果使用std::async就可能不创建新的线程。 // std::async也很容易拿到线程的返回值。 #include<iostream> #include<mutex> #include<thread> #include<future> using namespace std; int my_thread() { cout << "my_thread start... " << "thread id = " << std::this_thread::get_id() << endl; std::chrono::milliseconds dura(3000); std::this_thread::sleep_for(dura); cout << "my_thread end... " << "thread id = " << std::this_thread::get_id() << endl; return 5; } int main() { cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl; std::future< int > result = std::async(my_thread); cout << result.get() << endl; cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl; return 0; } //(2.3) std::async不确定性问题的解决 //可以用std::future的wait_for方法来判断线程到底有没有创建新的线程 #include<iostream> #include<mutex> #include<thread> #include<future> using namespace std; int my_thread() { cout << "my_thread start... " << "thread id = " << std::this_thread::get_id() << endl; std::chrono::milliseconds dura(3000); std::this_thread::sleep_for(dura); cout << "my_thread end... " << "thread id = " << std::this_thread::get_id() << endl; return 5; } int main() { cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl; std::future< int > result = std::async(my_thread); // std::future_status status = result.wait_for(0s); std::future_status status = result.wait_for(std::chrono::seconds(0)); //等待0秒 if (status == std::future_status::deferred) { cout << "no thread created..." << endl; } else { cout << "new thread start..." << endl; } cout << result.get() << endl; cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl; return 0; } |
posted on 2021-10-27 21:40 xcxfury001 阅读(38) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用