How to pass argument to thread pool function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to pass argument to thread pool function

I tried to create a sample thread pool as below: https://www.sololearn.com/compiler-playground/covRjcxiT4nn This is working fine, but when I try to add an int type input to func, it does not work as below: https://www.sololearn.com/compiler-playground/c44cZN9RJG8j

3rd Nov 2022, 12:20 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Answers
+ 6
https://code.sololearn.com/c3RkVtjM4U8G/?ref=app changes : line 43 ,46,108 you should pass function as argument not after evaluating it , as it will pass the returned value not function pointer to the thread pool ..
3rd Nov 2022, 1:07 PM
Prashanth Kumar
Prashanth Kumar - avatar
+ 4
Ketan Lalcheta actually i have no idea what youre trying to do ... but the point is you shouldn't pass function after evaluating ... for line 48 you could just store a map of std::function and its argument. for line 43 jobFunc(0) here we should pass in something since it cant accept void .. i dunno what youre trying to do.. explain further because im a bit lazy to read full code ...
3rd Nov 2022, 1:30 PM
Prashanth Kumar
Prashanth Kumar - avatar
3rd Nov 2022, 12:28 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks .. What about line 48 ? fn(i); Does it not call function ? We need to just pass it to queue...right ?
3rd Nov 2022, 1:22 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Also why 0 is passed on line 48 ? jobFunc(0); // arg should be int not void ...
3rd Nov 2022, 1:22 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I got your point of not passing an evalualted fun ... What i need to do is to call function as job with id I passed from main function
3rd Nov 2022, 2:03 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Basically for loop index i passed as argument should be used to call jobFun, not zero...how to get that int there
3rd Nov 2022, 2:06 PM
Ketan Lalcheta
Ketan Lalcheta - avatar