简体   繁体   中英

Why `std::thread()` and `std::packaged_task()` works different, although they both accept callable targets?

Here is a simple code snippet :

#include <thread>
#include <future>
#include <functional>

void foo(int){}

int main()
{
    std::thread(foo, 1).join();                  //works indeed
    std::packaged_task<void(int)> task{foo, 1};  //complian
    std::packaged_task<void(int)> task{std::bind(foo, 1)};
}

Both std::thread() and std::packaged_task() accept callable targets , why the code for std::packaged_task() is a little different? Why does not make the std::packged_task() works like std::thread() (ie accepts arguments like std::thread )?

The class template std::packaged_task wraps any Callable target (function, lambda expression, bind expression, or another function object) so that it can be invoked asynchronously. Its return value or exception thrown is stored in a shared state which can be accessed through std::future objects.

std::packaged_task does not run a function/callable immediately, a function execution is deferred and parameters can be passed later to void operator()( ArgTypes... args ); , therefore the possible constructor template< class Function, class... Args > explicit packaged_task( Function&& f, Args&&... args ); is not necessary.

However std::thread runs a function/callable immediately, thus parameters must be passed to the constructor template< class Function, class... Args > explicit thread( Function&& f, Args&&... args ); . You may consider it like sugar to avoid persistently writing code like std::thread(bind(f, 1)); instead of convenient std::thread(f, 1); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM