ThreadPool icon indicating copy to clipboard operation
ThreadPool copied to clipboard

changing the type of task queue from queue<function<void()>> to queue<packaged_task<void()>>

Open littlebirds opened this issue 7 years ago • 3 comments

the advantage will be that:

  1. in enqueue() method you no longer need to make a shared_ptr of packaged_task,
  2. tasks.emplace(task{ (*task)(); }); === ==> tasks.emplace(std::move(task))

The change works fine on g++ 6.2.0, so I wonder if current implementation is due to compatibility back then?

littlebirds avatar Feb 07 '18 16:02 littlebirds

Wow, this really works! I would have though that a packaged_task<int()> couldn't be moved into a packaged_task<void()>. Are there any disadvantages?

jhasse avatar Apr 18 '18 11:04 jhasse

the advantage will be that:

  1. in enqueue() method you no longer need to make a shared_ptr of packaged_task,
  2. tasks.emplace(task{ (*task)(); }); === ==> tasks.emplace(std::move(task))

The change works fine on g++ 6.2.0, so I wonder if current implementation is due to compatibility back then?

msvc will not compile... although it is a msvc bug.

WanpengQian avatar Apr 27 '21 13:04 WanpengQian

@littlebirds Unfortunately this implies task and tasks should be the same type. This means users will be able to enqueue callables with the same signature only. In the original implementation one can do:

pool.enqueue([](int a){return a*42;});
pool.enqueue([](string s){return "Hello"+s;});

In the proposed implementation it will be impossible since all the callables should be the same signature (return and parameters type).

RuslanSergeev avatar Jan 29 '23 21:01 RuslanSergeev