i'm trying to perform a thread pool design pattern in c++, but i'm stuck on passing a task function parameter in order to push it inside a list here is my code
std::list<std::function<void(int)>> work_queue;
my push function
void pushTask(std::function<void(int)> func , int a)
{
std::unique_lock<std::mutex> lck(wq_mutex);
work_queue.push_back(std::function<void(int)>(func),a));
}
and here my main function with the task functin
void calcul(int a){
std::cout << a << "\n";
}
int main(){
ThreadPool th(10);
th.pushTask(std::bind(&calcule,4));
return 0;
}
i getting an error in this line
work_queue.push_back(std::function<void(int)>(func),a));
can anyone identify the issue please ?
You doing that wrong.
std::list<std::function<void()>> work_queue; // you do not need int here
void pushTask(std::function<void()> func) // so here it is also obsolete
{
std::unique_lock<std::mutex> lck(wq_mutex);
work_queue.push_back(std::move(func));
}
// now this will be ok
th.pushTask(std::bind(&calcule,4));
// but since this is C++11 lambda is preferred
th.pushTask([]() { calcule(4); } );