What is the difference between packaged_task and async

后端 未结 3 1446
悲&欢浪女
悲&欢浪女 2020-11-30 17:02

While working with the threaded model of C++11, I noticed that

std::packaged_task task([](int a, int b) { return a + b; });
auto f = task         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 17:12

    Packaged Task vs async

    p> Packaged task holds a task [function or function object] and future/promise pair. When the task executes a return statement, it causes set_value(..) on the packaged_task's promise.

    a> Given Future, promise and package task we can create simple tasks without worrying too much about threads [thread is just something we give to run a task].

    However we need to consider how many threads to use or whether a task is best run on the current thread or on another etc.Such descisions can be handled by a thread launcher called async(), that decides whether to create a new a thread or recycle an old one or simply run the task on the current thread. It returns a future .

提交回复
热议问题