SlideShare a Scribd company logo
1 of 35
Download to read offline
Item 35:
Prefer task-based programming to
thread-based.
Item 36:
Specify std::launch::async if
asynchronicity is essential.
BE RATIONAL, NOT SOUR.
Tommy Kuo [:KuoE0]
kuoe0@mozilla.com
Effective Modern C++
Item 35:
Prefer task-based programming
to thread-based.
Concurrency in C++11
int doAsyncWork();
// thread-based programming
std::thread t(doAsyncWork);
// task-based programming
auto fut = std::async(doAsyncWork);
std::future<int>
Return Valueuse get() to get the return value
std::async
function
std::future
object
return
int fib(int x) {
return x <= 2 ? x - 1 : fib(x - 1) + fib(x -
2);
}
int main() {
auto fut = std::async(fib, 40);
auto ret = fut.get();
std::cout << ret << std::endl;
return 0;
}
std::future::get
function
call
Handle Erroruse get() to throw exceptions
std::async
function
std::future
object
return
int fib(int x) {
if (x < 1) throw logic_error("Don't you know
Fibonacci?");
return x <= 2 ? x - 1 : fib(x - 1) + fib(x - 2);
}
int main() {
auto fut = async(fib, 0);
try {
cout << fut.get() << endl;
} catch(exception& e) {
cout << e.what() << endl;
}
return 0;
}
std::future::get
function
call
throw exception
Handle Error with std::thread
int fib(int x) {
if (x < 1) throw logic_error("Don't you know Fibonacci?");
return x <= 2 ? x - 1 : fib(x - 1) + fib(x - 2);
}
int main() {
try {
auto t = thread(fib, 0);
} catch(exception& e) {
cout << e.what() << endl;
}
return 0;
}
throw exception
Program terminated! CC 2.0
Nightmare with Thread Management
(thread exhaustion)
system provides
6 threads.
int main() {
std::vector<std::thread> thread_pool(1000);
return 0;
}
throw std::system_error exception
more than system can provide
Nightmare with Thread Management
(oversubscription)
CPU provides
2 threads
system has
100 threads
ready to run
× 100
oversubscription
Nightmare with Thread Management
(oversubscription)
CPU provides
2 threads
system has
100 threads
ready to run
× 100
Context switches increase the
overall thread management
overhead of the system.
oversubscription
– Effective Modern C++, Scott Meyer
“Your life will be easier if you dump these problems
on somebody else, and using std::async.”
std::async takes responsibility for thread management.
#1, running task 1
#2, running task 2
#3, empty
task 3
task 4
task 5
task 6
task 7
task queue
Discuss in item 36 later :)
BUT
When to Use std::thread
- Need access to the API of the underlying threading
implementation.

Using std::thread::native_handle to get the lower-level platform-
specific thread (pthreads or Windows’ Threads).
- Need to and are able to optimize thread usage.

- Need to implement threading technology beyond the C++
concurrency API.
Things to Remember
- The std::thread API offers no direct way to get return values from
asynchronously run functions, and if those functions throw, the program is
terminated.
- Thread-based programming calls for manual management of thread
exhaustion, oversubscription, load balancing, and adaptation to new
platforms.
- Task-based programming via std::async with the default launch policy
handles most of these issues for you.
Item 36:
Specify std::launch::async if
asynchronicity is essential.
Launch Policy of std::async
- std::launch::async
Task must be run asynchronously.
- std::launch::deferred
Task may run only when get or wait is called on the future
returned by std::async.
You have to present
on 2016/01/06.
2015/10/01 2015/10/01
Junior
Done. 😎
2015/10/02 2016/01/05
I hope I can finish it
before Wednesday. 😭
You have to present
on 2016/01/06.
Junior
Tommy (with async-driven) Tommy (with deferred-driven)
behavior of async policy behavior of deferred policy
OK! 😎
Tommy (with async-driven)
OK! 😎
Tommy (with async-driven)
Are you ready?
Junior
Are you ready?
Junior
Default Launch Policy
auto fut = std::async(task);
// create with default launch policy
auto fut = std::async(std::launch::async |
std::launch::deferred, task);
// as same as the default launch policy
The default policy thus permits tasks to be run either
asynchronously or synchronously.
- Not possible to predict whether tasks will run concurrently.
- Not possible to predict whether tasks run on a thread
different from the thread invoking get or wait.
- May not be possible to predict whether tasks run at all.
std::async(task)
async policy
deferred policy
looks good!
thread_local variables
timeout-based wait
affect
With deferred policy,
thread_local variables will act as
normal global variables.
thread_local variables with async policy
thread_local int x = 0;
int func(bool update, int val){
return update ? x = val : x;
}
int main(){
x = 9;
std::future<int> task[4];
for (int i = 0; i < 3; ++i) task[i] =
std::async(std::launch::async, func, true, i + 1);
task[3] = std::async(std::launch::async, func, false, 0);
for (auto& t: task) std::cout << t.get();
std::cout << x << std::endl;
return 0;
}
thread_local variables with async policy
thread_local int x = 0;
int func(bool update, int val){
return update ? x = val : x;
}
int main(){
x = 9;
std::future<int> task[4];
for (int i = 0; i < 3; ++i) task[i] =
std::async(std::launch::async, func, true, i + 1);
task[3] = std::async(std::launch::async, func, false, 0);
for (auto& t: task) std::cout << t.get();
std::cout << x << std::endl;
return 0;
}
output
——————————————————————
12309
thread_local variables with deferred policy
thread_local int x = 0;
int func(bool update, int val){
return update ? x = val : x;
}
int main(){
x = 9;
std::future<int> task[4];
for (int i = 0; i < 3; ++i) task[i] =
std::async(std::launch::deferred, func, true, i + 1);
task[3] = std::async(std::launch::deferred, func, false, 0);
for (auto& t: task) std::cout << t.get();
std::cout << x << std::endl;
return 0;
}
thread_local variables with deferred policy
thread_local int x = 0;
int func(bool update, int val){
return update ? x = val : x;
}
int main(){
x = 9;
std::future<int> task[4];
for (int i = 0; i < 3; ++i) task[i] =
std::async(std::launch::deferred, func, true, i + 1);
task[3] = std::async(std::launch::deferred, func, false, 0);
for (auto& t: task) std::cout << t.get();
std::cout << x << std::endl;
return 0;
}
output
——————————————————————
12333
With deferred policy, wait_for
and wait_until will return
std::future_status::deferred.
Infinite-loop problem with timeout-based wait
int main() {
auto fut = std::async(std::launch::deferred,
task); // deferred policy
while (fut.wait_for(100ms) !=
std::future_status::ready) {
std::cout << “waiting…” << std::endl;
}
std::cout << fut.get() << std::endl;
return 0;
}
return std::future_status::deferred always
Resolve infinite-loop problem
int main() {
auto fut = std::async(task); // deferred policy
if (fut.wait_for(0ms) ==
std::future_status::deferred) {
fut.wait(); // fut.get() also works.
} else {
while (fut.wait_for(100ms) !=
std::future_status::ready) {
std::cout << “waiting…” << std::endl;
}
}
std::cout << fut.get() << std::endl;
return 0;
}
check it and make it
run synchronously
BUT
When to Use Default Launch Policy
- The task need not run concurrently with the thread calling get or wait. 

- It doesn’t matter which thread’s thread_local variables are read or written. 

- Either there’s a guarantee that get or wait will be called on the future
returned by std::async.

- It’s acceptable that the task may never execute.

- Code using wait_for or wait_until takes the possibility of deferred status
into account.
Function to Use Async Policy (C++11)
template<typename F, typename... Ts>
inline
std::future<typename std::result_of<F(Ts…)>::type>
reallyAsync(F&& f, Ts&&... params)
{
return std::async(std::launch::async,
std::forward<F>(f),
std::forward<Ts>(params)…);
}
Function to Use Async Policy (C++14)
template<typename F, typename... Ts>
inline
auto
reallyAsync(F&& f, Ts&&... params)
{
return std::async(std::launch::async,
std::forward<F>(f),
std::forward<Ts>(params)…);
}
Things to Remember
- The default launch policy for std::async permits both
asynchronous and synchronous task execution.
- This flexibility leads to uncertainty when accessing thread_local
variables, implies that the task may never execute, and affects
program logic for timeout-based wait calls.
- Specify std::launch::async if asynchronous task execution is
essential.
Thanks.
CC-BY-SA

More Related Content

What's hot

Qt multi threads
Qt multi threadsQt multi threads
Qt multi threadsYnon Perek
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic AnalysisAndres Mendez-Vazquez
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? ICS
 
Nginx internals
Nginx internalsNginx internals
Nginx internalsliqiang xu
 
Effective Modern C++ Item 9 and 10
Effective Modern C++ Item 9 and 10Effective Modern C++ Item 9 and 10
Effective Modern C++ Item 9 and 10uchan_nos
 
Apache Giraph: Large-scale graph processing done better
Apache Giraph: Large-scale graph processing done betterApache Giraph: Large-scale graph processing done better
Apache Giraph: Large-scale graph processing done better🧑‍💻 Manuel Coppotelli
 
Basics of Model/View Qt programming
Basics of Model/View Qt programmingBasics of Model/View Qt programming
Basics of Model/View Qt programmingICS
 
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...Igalia
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Mario Fusco
 
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기흥배 최
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction SelectionShiva Chen
 
A python web service
A python web serviceA python web service
A python web serviceTemian Vlad
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4ICS
 
Linux Linux Traffic Control
Linux Linux Traffic ControlLinux Linux Traffic Control
Linux Linux Traffic ControlSUSE Labs Taipei
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explainedPIYUSH Dubey
 

What's hot (20)

Qt multi threads
Qt multi threadsQt multi threads
Qt multi threads
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong?
 
Nginx internals
Nginx internalsNginx internals
Nginx internals
 
Effective Modern C++ Item 9 and 10
Effective Modern C++ Item 9 and 10Effective Modern C++ Item 9 and 10
Effective Modern C++ Item 9 and 10
 
GCC LTO
GCC LTOGCC LTO
GCC LTO
 
Apache Giraph: Large-scale graph processing done better
Apache Giraph: Large-scale graph processing done betterApache Giraph: Large-scale graph processing done better
Apache Giraph: Large-scale graph processing done better
 
Basics of Model/View Qt programming
Basics of Model/View Qt programmingBasics of Model/View Qt programming
Basics of Model/View Qt programming
 
Snort
SnortSnort
Snort
 
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
 
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
KGC 2016 오픈소스 네트워크 엔진 Super socket 사용하기
 
LLVM Instruction Selection
LLVM Instruction SelectionLLVM Instruction Selection
LLVM Instruction Selection
 
A python web service
A python web serviceA python web service
A python web service
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4
 
Linux Linux Traffic Control
Linux Linux Traffic ControlLinux Linux Traffic Control
Linux Linux Traffic Control
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explained
 

Viewers also liked

Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in RustChih-Hsuan Kuo
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。Chih-Hsuan Kuo
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSChih-Hsuan Kuo
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoChih-Hsuan Kuo
 
What you need to know to start developing WebVR
What you need to know to start developing WebVRWhat you need to know to start developing WebVR
What you need to know to start developing WebVRArturo Paracuellos
 
20140531 serebryany lecture01_fantastic_cpp_bugs
20140531 serebryany lecture01_fantastic_cpp_bugs20140531 serebryany lecture01_fantastic_cpp_bugs
20140531 serebryany lecture01_fantastic_cpp_bugsComputer Science Club
 
Threading Bobbin Tutorial
Threading Bobbin TutorialThreading Bobbin Tutorial
Threading Bobbin TutorialRandi Burford
 
Async await in C++
Async await in C++Async await in C++
Async await in C++cppfrug
 
Pixel-Lab / Games:EDU / Michel Kripalani / Games Industry Overview and Trends
Pixel-Lab / Games:EDU / Michel Kripalani / Games Industry Overview and TrendsPixel-Lab / Games:EDU / Michel Kripalani / Games Industry Overview and Trends
Pixel-Lab / Games:EDU / Michel Kripalani / Games Industry Overview and Trendspixellab
 
David Shastry Experience Design Portfolio
David Shastry Experience Design PortfolioDavid Shastry Experience Design Portfolio
David Shastry Experience Design PortfolioDavid Shastry
 
Getting Started in VR with JS
Getting Started in VR with JSGetting Started in VR with JS
Getting Started in VR with JSRudy Jahchan
 
Introductions of Messaging bot 做聊天機器人
Introductions of Messaging bot 做聊天機器人Introductions of Messaging bot 做聊天機器人
Introductions of Messaging bot 做聊天機器人Johnny Sung
 
Artificial Intelligence in Computer and Video Games
Artificial Intelligence in Computer and Video GamesArtificial Intelligence in Computer and Video Games
Artificial Intelligence in Computer and Video GamesLuke Dicken
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Morris Singer
 

Viewers also liked (20)

Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
What you need to know to start developing WebVR
What you need to know to start developing WebVRWhat you need to know to start developing WebVR
What you need to know to start developing WebVR
 
Nug2004 yhe
Nug2004 yheNug2004 yhe
Nug2004 yhe
 
20140531 serebryany lecture01_fantastic_cpp_bugs
20140531 serebryany lecture01_fantastic_cpp_bugs20140531 serebryany lecture01_fantastic_cpp_bugs
20140531 serebryany lecture01_fantastic_cpp_bugs
 
Threading Bobbin Tutorial
Threading Bobbin TutorialThreading Bobbin Tutorial
Threading Bobbin Tutorial
 
Mnk hsa ppt
Mnk hsa pptMnk hsa ppt
Mnk hsa ppt
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Async await in C++
Async await in C++Async await in C++
Async await in C++
 
Pixel-Lab / Games:EDU / Michel Kripalani / Games Industry Overview and Trends
Pixel-Lab / Games:EDU / Michel Kripalani / Games Industry Overview and TrendsPixel-Lab / Games:EDU / Michel Kripalani / Games Industry Overview and Trends
Pixel-Lab / Games:EDU / Michel Kripalani / Games Industry Overview and Trends
 
David Shastry Experience Design Portfolio
David Shastry Experience Design PortfolioDavid Shastry Experience Design Portfolio
David Shastry Experience Design Portfolio
 
AMC Minor Technical Issues
AMC Minor Technical IssuesAMC Minor Technical Issues
AMC Minor Technical Issues
 
Getting Started in VR with JS
Getting Started in VR with JSGetting Started in VR with JS
Getting Started in VR with JS
 
Introductions of Messaging bot 做聊天機器人
Introductions of Messaging bot 做聊天機器人Introductions of Messaging bot 做聊天機器人
Introductions of Messaging bot 做聊天機器人
 
C++11
C++11C++11
C++11
 
C++11
C++11C++11
C++11
 
Artificial Intelligence in Computer and Video Games
Artificial Intelligence in Computer and Video GamesArtificial Intelligence in Computer and Video Games
Artificial Intelligence in Computer and Video Games
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015
 

Similar to Specify Async Policy for Concurrency with std::async

Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio Zoppi
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...corehard_by
 
A deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleA deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleSaúl Ibarra Corretgé
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++Dimitrios Platis
 
PythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummiesPythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummiesTatiana Al-Chueyr
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2Zaar Hai
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構Bo-Yi Wu
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Satalia
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialJin-Hwa Kim
 

Similar to Specify Async Policy for Concurrency with std::async (20)

Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
 
lecture56.ppt
lecture56.pptlecture56.ppt
lecture56.ppt
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
 
functions
functionsfunctions
functions
 
A deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio moduleA deep dive into PEP-3156 and the new asyncio module
A deep dive into PEP-3156 and the new asyncio module
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
Refactoring for testability c++
Refactoring for testability c++Refactoring for testability c++
Refactoring for testability c++
 
PythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummiesPythonBrasil[8] - CPython for dummies
PythonBrasil[8] - CPython for dummies
 
Function
FunctionFunction
Function
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
Profiling in Python
Profiling in PythonProfiling in Python
Profiling in Python
 
Async fun
Async funAsync fun
Async fun
 
Function
FunctionFunction
Function
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorial
 

More from Chih-Hsuan Kuo

[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-selectChih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in GeckoChih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-upChih-Hsuan Kuo
 

More from Chih-Hsuan Kuo (20)

Rust
RustRust
Rust
 
[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
 
[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up
 
[ACM-ICPC] About I/O
[ACM-ICPC] About I/O[ACM-ICPC] About I/O
[ACM-ICPC] About I/O
 

Recently uploaded

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

Specify Async Policy for Concurrency with std::async

  • 1. Item 35: Prefer task-based programming to thread-based. Item 36: Specify std::launch::async if asynchronicity is essential. BE RATIONAL, NOT SOUR. Tommy Kuo [:KuoE0] kuoe0@mozilla.com Effective Modern C++
  • 2. Item 35: Prefer task-based programming to thread-based.
  • 3. Concurrency in C++11 int doAsyncWork(); // thread-based programming std::thread t(doAsyncWork); // task-based programming auto fut = std::async(doAsyncWork); std::future<int>
  • 4. Return Valueuse get() to get the return value std::async function std::future object return int fib(int x) { return x <= 2 ? x - 1 : fib(x - 1) + fib(x - 2); } int main() { auto fut = std::async(fib, 40); auto ret = fut.get(); std::cout << ret << std::endl; return 0; } std::future::get function call
  • 5. Handle Erroruse get() to throw exceptions std::async function std::future object return int fib(int x) { if (x < 1) throw logic_error("Don't you know Fibonacci?"); return x <= 2 ? x - 1 : fib(x - 1) + fib(x - 2); } int main() { auto fut = async(fib, 0); try { cout << fut.get() << endl; } catch(exception& e) { cout << e.what() << endl; } return 0; } std::future::get function call throw exception
  • 6. Handle Error with std::thread int fib(int x) { if (x < 1) throw logic_error("Don't you know Fibonacci?"); return x <= 2 ? x - 1 : fib(x - 1) + fib(x - 2); } int main() { try { auto t = thread(fib, 0); } catch(exception& e) { cout << e.what() << endl; } return 0; } throw exception
  • 8. Nightmare with Thread Management (thread exhaustion) system provides 6 threads. int main() { std::vector<std::thread> thread_pool(1000); return 0; } throw std::system_error exception more than system can provide
  • 9. Nightmare with Thread Management (oversubscription) CPU provides 2 threads system has 100 threads ready to run × 100 oversubscription
  • 10. Nightmare with Thread Management (oversubscription) CPU provides 2 threads system has 100 threads ready to run × 100 Context switches increase the overall thread management overhead of the system. oversubscription
  • 11. – Effective Modern C++, Scott Meyer “Your life will be easier if you dump these problems on somebody else, and using std::async.”
  • 12. std::async takes responsibility for thread management. #1, running task 1 #2, running task 2 #3, empty task 3 task 4 task 5 task 6 task 7 task queue Discuss in item 36 later :)
  • 13. BUT
  • 14. When to Use std::thread - Need access to the API of the underlying threading implementation. Using std::thread::native_handle to get the lower-level platform- specific thread (pthreads or Windows’ Threads). - Need to and are able to optimize thread usage. - Need to implement threading technology beyond the C++ concurrency API.
  • 15. Things to Remember - The std::thread API offers no direct way to get return values from asynchronously run functions, and if those functions throw, the program is terminated. - Thread-based programming calls for manual management of thread exhaustion, oversubscription, load balancing, and adaptation to new platforms. - Task-based programming via std::async with the default launch policy handles most of these issues for you.
  • 16. Item 36: Specify std::launch::async if asynchronicity is essential.
  • 17. Launch Policy of std::async - std::launch::async Task must be run asynchronously. - std::launch::deferred Task may run only when get or wait is called on the future returned by std::async.
  • 18. You have to present on 2016/01/06. 2015/10/01 2015/10/01 Junior Done. 😎 2015/10/02 2016/01/05 I hope I can finish it before Wednesday. 😭 You have to present on 2016/01/06. Junior Tommy (with async-driven) Tommy (with deferred-driven) behavior of async policy behavior of deferred policy OK! 😎 Tommy (with async-driven) OK! 😎 Tommy (with async-driven) Are you ready? Junior Are you ready? Junior
  • 19. Default Launch Policy auto fut = std::async(task); // create with default launch policy auto fut = std::async(std::launch::async | std::launch::deferred, task); // as same as the default launch policy The default policy thus permits tasks to be run either asynchronously or synchronously.
  • 20. - Not possible to predict whether tasks will run concurrently. - Not possible to predict whether tasks run on a thread different from the thread invoking get or wait. - May not be possible to predict whether tasks run at all.
  • 21. std::async(task) async policy deferred policy looks good! thread_local variables timeout-based wait affect
  • 22. With deferred policy, thread_local variables will act as normal global variables.
  • 23. thread_local variables with async policy thread_local int x = 0; int func(bool update, int val){ return update ? x = val : x; } int main(){ x = 9; std::future<int> task[4]; for (int i = 0; i < 3; ++i) task[i] = std::async(std::launch::async, func, true, i + 1); task[3] = std::async(std::launch::async, func, false, 0); for (auto& t: task) std::cout << t.get(); std::cout << x << std::endl; return 0; }
  • 24. thread_local variables with async policy thread_local int x = 0; int func(bool update, int val){ return update ? x = val : x; } int main(){ x = 9; std::future<int> task[4]; for (int i = 0; i < 3; ++i) task[i] = std::async(std::launch::async, func, true, i + 1); task[3] = std::async(std::launch::async, func, false, 0); for (auto& t: task) std::cout << t.get(); std::cout << x << std::endl; return 0; } output —————————————————————— 12309
  • 25. thread_local variables with deferred policy thread_local int x = 0; int func(bool update, int val){ return update ? x = val : x; } int main(){ x = 9; std::future<int> task[4]; for (int i = 0; i < 3; ++i) task[i] = std::async(std::launch::deferred, func, true, i + 1); task[3] = std::async(std::launch::deferred, func, false, 0); for (auto& t: task) std::cout << t.get(); std::cout << x << std::endl; return 0; }
  • 26. thread_local variables with deferred policy thread_local int x = 0; int func(bool update, int val){ return update ? x = val : x; } int main(){ x = 9; std::future<int> task[4]; for (int i = 0; i < 3; ++i) task[i] = std::async(std::launch::deferred, func, true, i + 1); task[3] = std::async(std::launch::deferred, func, false, 0); for (auto& t: task) std::cout << t.get(); std::cout << x << std::endl; return 0; } output —————————————————————— 12333
  • 27. With deferred policy, wait_for and wait_until will return std::future_status::deferred.
  • 28. Infinite-loop problem with timeout-based wait int main() { auto fut = std::async(std::launch::deferred, task); // deferred policy while (fut.wait_for(100ms) != std::future_status::ready) { std::cout << “waiting…” << std::endl; } std::cout << fut.get() << std::endl; return 0; } return std::future_status::deferred always
  • 29. Resolve infinite-loop problem int main() { auto fut = std::async(task); // deferred policy if (fut.wait_for(0ms) == std::future_status::deferred) { fut.wait(); // fut.get() also works. } else { while (fut.wait_for(100ms) != std::future_status::ready) { std::cout << “waiting…” << std::endl; } } std::cout << fut.get() << std::endl; return 0; } check it and make it run synchronously
  • 30. BUT
  • 31. When to Use Default Launch Policy - The task need not run concurrently with the thread calling get or wait. 
 - It doesn’t matter which thread’s thread_local variables are read or written. 
 - Either there’s a guarantee that get or wait will be called on the future returned by std::async. - It’s acceptable that the task may never execute. - Code using wait_for or wait_until takes the possibility of deferred status into account.
  • 32. Function to Use Async Policy (C++11) template<typename F, typename... Ts> inline std::future<typename std::result_of<F(Ts…)>::type> reallyAsync(F&& f, Ts&&... params) { return std::async(std::launch::async, std::forward<F>(f), std::forward<Ts>(params)…); }
  • 33. Function to Use Async Policy (C++14) template<typename F, typename... Ts> inline auto reallyAsync(F&& f, Ts&&... params) { return std::async(std::launch::async, std::forward<F>(f), std::forward<Ts>(params)…); }
  • 34. Things to Remember - The default launch policy for std::async permits both asynchronous and synchronous task execution. - This flexibility leads to uncertainty when accessing thread_local variables, implies that the task may never execute, and affects program logic for timeout-based wait calls. - Specify std::launch::async if asynchronous task execution is essential.