std::async基本用法

std::async基本用法

参考:

https://www.cnblogs.com/moodlxs/p/10111601.html

https://www.yht7.com/news/29953

std::async的启动策略类型是个枚举类enum class launch,包括:

std::launch::deferred:表示入口函数调用被延迟到std::future的wait或get才执行,如果没调用wait和get,线程则不会创建,即便是调用了get和wait也不会创建新新线程,函数的调用是主线程调用的

std::launch::async:在调用async的时候就创建新线程,系统默认传的参数

注意:

  • std::future::get只能调用一次,多次调用会返回异常
  • 主线程使用std::future::get获取结果,如果调用过程中,任务尚未完成,则主线程阻塞至任务完成
  • std::launch::deferred|std::launch::async这种组合没有实际起作用,输入这样的参数,起作用的只有std::launch::async
原文地址:https://www.cnblogs.com/huanyinglvtuan/p/13597783.html