Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does the function with arguments have errors? #23

Open
mirro187 opened this issue May 16, 2019 · 2 comments
Open

Why does the function with arguments have errors? #23

mirro187 opened this issue May 16, 2019 · 2 comments

Comments

@mirro187
Copy link

Error C2672: "CTPL ::thread_pool::push" : no matching overloaded function found
Error C2780: "STD ::future : CTPL ::thread_pool::push(F &&)" : one parameter should be input, but two are provided
Error C2893: function template "STD ::future CTPL ::thread_pool::push(F &&,Rest &&...) "specialty
Error C2672: "STD ::vector< STD ::future, STD ::allocator<_Ty> : >::emplace_back" : overloaded function not found

#include "ctpl_stl.h"
#include "iostream"
#include "string"

void f(int j)
{
std::cout << j << std::endl;

}

int main(int argc, char **argv) {

ctpl::thread_pool p(8 /* two threads in the pool */);

std::vector< std::future<int> > results;

for (int i = 0; i < 8; ++i) {
	results.emplace_back(
		p.push(f, i));
}

for (auto && result : results)
	std::cout << result.get() << ' ';
std::cout << std::endl;
getchar();

return 0;

}

@mirro187
Copy link
Author

What's the right thing to do?

@nicolas-chan-42
Copy link

nicolas-chan-42 commented Jan 1, 2020

Hi I am new to CTPL too but I think that I have the answer to your question.

For functions to be pushed into thread_pool, there has to be an int argument as its first argument, the rest being the actual arguments needed by your function. So it translate to the following:

void f(int j)
should become
void f(int id, int j)

Then your program should work.

For functions with no argument by itself, e.g.

void print_something()
{
    std::cout << "HelloWorld!";
}

You will need to make it (in CTPL version):

void print_something(int id)  // Notice the added argument.
{
    std::cout << "HelloWorld!";
}

This seems to be applicable to all kinds of functions that you will passed into thread_pool, i.e. you need an int argument as the first argument, regardless of functor, function pointer, lambda, etc.

Hope this helps...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants