packaged-task

Why std::future is different returned from std::packaged_task and std::async?

早过忘川 提交于 2020-12-05 07:01:30
问题 I got to know the reason that future returned from std::async has some special shared state through which wait on returned future happened in the destructor of future. But when we use std::pakaged_task , its future does not exhibit the same behavior. To complete a packaged task, you have to explicitly call get() on future object from packaged_task . Now my questions are: What could be the internal implementation of future (thinking std::async vs std::packaged_task )? Why the same behavior was

C2248 error when using promise

若如初见. 提交于 2019-12-25 04:24:11
问题 The code below triggers the error: Error 1 error C2248: 'std::promise<_Ty>::promise' : cannot access private member declared in class 'std::promise<_Ty>' How can I fix it? Thanks a lot ! #define _parallel_qick_sort #ifdef _parallel_qick_sort #include <boost/shared_ptr.hpp> #include <thread> #include <vector> #include <list> #include <future> #include <atomic> #include "ThreadSafeStack.hpp" using namespace std; template<typename T> struct sorter { struct chunk_to_sort { std::list<T> data_m;

What is the difference between packaged_task and async

一个人想着一个人 提交于 2019-12-17 08:02:05
问题 While working with the threaded model of C++11, I noticed that std::packaged_task<int(int,int)> task([](int a, int b) { return a + b; }); auto f = task.get_future(); task(2,3); std::cout << f.get() << '\n'; and auto f = std::async(std::launch::async, [](int a, int b) { return a + b; }, 2, 3); std::cout << f.get() << '\n'; seem to do exactly the same thing. I understand that there could be a major difference if I ran std::async with std::launch::deferred , but is there one in this case? What

Implementing a simple, generic thread pool in C++11

試著忘記壹切 提交于 2019-12-09 04:55:31
问题 I want to create a thread pool for experimental purposes (and for the fun factor). It should be able to process a wide variety of tasks (so I can possibly use it in later projects). In my thread pool class I'm going to need some sort of task queue. Since the Standard Library provides std::packaged_task since the C++11 standard, my queue will look like std::deque<std::packaged_task<?()> > task_queue , so the client can push std::packaged_task s into the queue via some sort of public interface

Implementing a simple, generic thread pool in C++11

旧街凉风 提交于 2019-12-03 04:41:20
I want to create a thread pool for experimental purposes (and for the fun factor). It should be able to process a wide variety of tasks (so I can possibly use it in later projects). In my thread pool class I'm going to need some sort of task queue. Since the Standard Library provides std::packaged_task since the C++11 standard, my queue will look like std::deque<std::packaged_task<?()> > task_queue , so the client can push std::packaged_task s into the queue via some sort of public interface function (and then one of the threads in the pool will be notified with a condition variable to execute

Why is std::packaged_task<void()> not valid?

霸气de小男生 提交于 2019-12-01 03:17:12
问题 Using MSVC2012, The following code will compile and run as expected std::packaged_task< int() > task( []()->int{ std::cout << "hello world" << std::endl; return 0; } ); std::thread t( std::move(task) ); t.join(); while the following code will fail to compile and run std::packaged_task< void() > task( [](){ std::cout << "hello world" << std::endl; } ); std::thread t( std::move(task) ); t.join(); Why is this so? Edit: As a workaround, it is possible to use std::promise to get a std::future on a

When to use promise over async or packaged_task?

橙三吉。 提交于 2019-11-30 10:13:55
问题 When should I use std::promise over std::async or std::packaged_task ? Can you give me practical examples of when to use each one of them? 回答1: std::async std::async is a neat and easy way to get a std::future, but: It does not always it start a new thread; pass std::launch::async as a first parameter to force it. auto f = std::async( std::launch::async, func ); The std::~future destructor can block until the new thread finishes auto sleep = [](int s) { std::this_thread::sleep_for(std::chrono

When to use promise over async or packaged_task?

ぃ、小莉子 提交于 2019-11-29 19:26:53
When should I use std::promise over std::async or std::packaged_task ? Can you give me practical examples of when to use each one of them? user2622016 std::async std::async is a neat and easy way to get a std::future , but: It does not always it start a new thread; pass std::launch::async as a first parameter to force it. auto f = std::async( std::launch::async, func ); The std::~future destructor can block until the new thread finishes auto sleep = [](int s) { std::this_thread::sleep_for(std::chrono::seconds(s)); }; { auto f = std::async( std::launch::async, sleep, 5 ); } Normally we expect

What is the difference between packaged_task and async

懵懂的女人 提交于 2019-11-27 05:52:54
While working with the threaded model of C++11, I noticed that std::packaged_task<int(int,int)> task([](int a, int b) { return a + b; }); auto f = task.get_future(); task(2,3); std::cout << f.get() << '\n'; and auto f = std::async(std::launch::async, [](int a, int b) { return a + b; }, 2, 3); std::cout << f.get() << '\n'; seem to do exactly the same thing. I understand that there could be a major difference if I ran std::async with std::launch::deferred , but is there one in this case? What is the difference between these two approaches, and more importantly, in what use cases should I use one