摘要: std::async 在简单的 IO 上比 std::thread 更有优势 前提:如果我们只需要一些异步执行的代码,这样不会阻塞主线程的执行,最好的办法是使用 std::async 来执行这些代码。这些操作我们也可以使用 std::thread 创建线程并通过函数指针 或者 lambda 参数将可 阅读全文
posted @ 2022-05-28 16:04 strive-sun 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 不要在对时间敏感的上下文中使用 .get() 先看下面的代码, #include "stdafx.h" #include <future> #include <iostream> int main() { std::future<int> myFuture = std::async(std::lau 阅读全文
posted @ 2022-05-28 15:31 strive-sun 阅读(87) 评论(0) 推荐(0) 编辑
摘要: 前提: C++ 11 中提供了多线程的标准库,提供了管理线程、保护共享数据、线程间同步操作、原子操作等类。多线程库对应的头文件是 #include <thread>,类名为 std::thread。 然而线程毕竟是比较贴近系统的东西,使用起来仍然不是很方便,特别是线程同步及获取线程运行结果上就更加麻 阅读全文
posted @ 2022-05-28 14:38 strive-sun 阅读(897) 评论(0) 推荐(0) 编辑