progschj / ThreadPool

A simple C++11 Thread Pool implementation
zlib License
7.85k stars 2.24k forks source link

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

Open littlebirds opened 6 years ago

littlebirds commented 6 years ago

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?

jhasse commented 6 years ago

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?

WanpengQian commented 3 years ago

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.

RuslanSergeev commented 1 year ago

@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).