Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea3
Effective Modern C++ StudyC++ Korea4
Effective Modern C++ StudyC++ Korea6
Effective Modern C++ StudyC++ Korea7
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea9
Effective Modern C++ StudyC++ Korea10
Effective Modern C++ StudyC++ Korea
• Template으로 된 parameter 처리를 C++ 11에서 할 때
• C++ 14에서는 auto 키워드 지원으로 필요 없다.
Effective Modern C++ StudyC++ Korea
int doAsyncWork(); // this is considered as a task
std::thread t(doAsyncWork); // thread-basedauto fut = std::async(doAsyncWork); // task-based
// fut is future object for this task
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
int doAsyncWork() noexcept; // see Item 14 for noexceptstd::thread t(doAsyncWork); // throws if no more// threads are available
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
- thread-based 사례와 다르게 exception을 발생시키지 않음.
- STL 내부의 scheduler에서 task를 실행할 때 thread 생성 여부를 결정
(thread를 생성하지 않으면 task를 요청한 thread에서 해당 task 실행)
- thread 생성을 줄이므로 oversubscription 문제도 완화
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
auto fut1 = std::async(f); // run f using// default launch// policy
auto fut2 = std::async(std::launch::async | // run f eitherstd::launch::deferred, // async orf); // deferred
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
auto fut = std::async(f); // run f using default launch policy
Effective Modern C++ StudyC++ Korea
thread_local unsigned int i = 1;
int main() {auto fut = std::async(f);
}
Effective Modern C++ StudyC++ Korea
using namespace std::literals; // for C++14 duration// suffixes; see Item 34
void f() // f sleeps for 1 second,{ // then returns
std::this_thread::sleep_for(1s);}auto fut = std::async(f); // run f asynchronously
// (conceptually)while (fut.wait_for(100ms) != // loop until f has
std::future_status::ready) // finished running...{ // which may never happen!…}
Effective Modern C++ StudyC++ Korea
auto fut = std::async(f); // as aboveif (fut.wait_for(0s) == // if task is
std::future_status::deferred) // deferred...{
// ...use wait or get on fut… // to call f synchronously
} else { // task isn't deferredwhile (fut.wait_for(100ms) != // infinite loop not
std::future_status::ready) { // possible (assuming// f finishes)
… // task is neither deferred nor ready,// so do concurrent work until it's ready
}… // fut is ready}
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea
auto fut = std::async(std::launch::async, f); // launch f// asynchronously
Effective Modern C++ StudyC++ Korea
auto fut = std::async(std::launch::async, f); // launch f// asynchronously
Effective Modern C++ StudyC++ Korea
template<typename F, typename... Ts>inlinestd::future<typename std::result_of<F(Ts...)>::type>reallyAsync(F&& f, Ts&&... params) // return future{ // for asynchronous
return std::async(std::launch::async, // call to f(params...)std::forward<F>(f),std::forward<Ts>(params)...);
}
Effective Modern C++ StudyC++ Korea
template<typename F, typename... Ts>inlineauto // C++14reallyAsync(F&& f, Ts&&... params){
return std::async(std::launch::async,std::forward<F>(f),std::forward<Ts>(params)...);
}
Effective Modern C++ StudyC++ Korea