+ 1

Is packaged task not in thread

Hi Does package task not executed in different thread? Is it possible to get it executed in separate thread ? https://sololearn.com/compiler-playground/cktXscBZYYAn/?ref=app

1st Nov 2024, 10:40 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 odpowiedzi
+ 3
like this? #include <iostream> #include <future> #include <thread> int getValue(int n) { std::cout << "from getValue: " << std::this_thread::get_id() << '\n' << n <<'\n'; return n; } int main() { std::packaged_task<int(int)> pg(getValue); std::thread t1(move(pg), 11); t1.join(); std::packaged_task<int(int)> pg2(getValue); pg2(12); std::future<int> f = pg2.get_future(); std::cout << "from main: " << std::this_thread::get_id() << '\n' << f.get() << '\n'; } related articles: https://stackoverflow.com/questions/20306335/c11-packaged-task-running-with-its-own-thread-need-a-join-in-order-to-get-fu https://dev.to/clightning/good-bad-ugly-in-concurrent-programming-with-c-30ke#:~:text=The%20std%3A%3Apackaged_task%20is,the%20execution%20of%20the%20task. https://stackoverflow.com/questions/18143661/what-is-the-difference-between-packaged-task-and-async
1st Nov 2024, 11:36 PM
Bob_Li
Bob_Li - avatar
+ 1
Thanks Bob_Li
3rd Nov 2024, 8:19 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
I wrongly assumed that packaged task is always a different thread
3rd Nov 2024, 8:19 AM
Ketan Lalcheta
Ketan Lalcheta - avatar