-3

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 ?

Lyes
  • 400
  • 5
  • 19
  • 2
    Avoid using that sort of cast, it should be `work_queue.push_back(func, a);`. In your example you have mismatched parentheses. If you are still having trouble then post a [MCVE](http://stackoverflow.com/help/mcve) – M.M Nov 18 '18 at 20:42
  • 1
    When the compiler gives you an error, it's important to read it carefully. If you don't understand it and want to post a whole Stack Overflow question about it, the minimum you should do is include the error message with your code. – paddy Nov 18 '18 at 20:44
  • whats the error say? I think I count an extra `)`, is that the problem? – kmdreko Nov 18 '18 at 20:44
  • i changed the error line to work_queue.push_back(func,a); and i'm still getting the error: no matching function error – Lyes Nov 18 '18 at 20:46
  • Note that the result of `std::bind(&calcule,4)`is not implicitly convertible to `std::function` since you explicitly bind the parameter `4` -- its \`effective' type is `std::function`. – G.M. Nov 18 '18 at 20:46
  • What is the purpose of the second argument to `pushTask`? You are not invoking it with 2 arguments, and you have already bound an argument to `func`. – paddy Nov 18 '18 at 20:47
  • @G.M. so how should i modify my code ? – Lyes Nov 18 '18 at 20:50

1 Answers1

0

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); } );
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • it works ,thanks , what's the std::move role please ? – Lyes Nov 18 '18 at 20:55
  • read about move schematics in C++11. – Marek R Nov 18 '18 at 20:56
  • Anyway since it looks like you are a newbie I recommend you to avoid use of threads until you greatly improve your skills. I know threads looks very fancy and fashionable, but they are hard to master. Biggest issue use of threads are not deterministic. So you can think that created nice pice of software since it works on your machine, but there is bug which will happen always on some machine. – Marek R Nov 18 '18 at 20:58