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? #26

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

Why does the function with arguments have errors? #26

mirro187 opened this issue May 16, 2019 · 16 comments

Comments

@mirro187
Copy link

mirro187 commented May 16, 2019

error C2660 “std::packaged_task<int (int)>::operator ()”: The function does not take zero arguments

#include "thread_pool.hpp"
#include  "thread"
#include "future"
#include "functional"
#include "memory"
#include "iostream"
using namespace std;

int f(int j)
{
	return j;
}

int main(int argc, char **argv)
{
	tp::ThreadPool pool;
	//
	std::packaged_task<int(int)> t(f);
	std::future<int> r = t.get_future();
	pool.post(t);
	r.get();
	//while (1);
	getchar();
	return 0;
}
@mirro187
Copy link
Author

What's the right thing to do?

@vittorioromeo
Copy link
Collaborator

Use a lambda to decide how your function is going to be invoked. E.g.

pool.post([]{ t(5); });

@mirro187
Copy link
Author

Use a lambda to decide how your function is going to be invoked. E.g.

pool.post([]{ t(5); });

Just test pool.post([]{ t(5); }); code and there's an error occurred.

Error C3493: "t" cannot be caught implicitly because the default capture mode has not been specified

#include "thread_pool.hpp"
#include "thread"
#include "future"
#include "functional"
#include "memory"
#include "iostream"
using namespace std;

int f(int j)
{
return j;
}

int main(int argc, char **argv)
{
tp::ThreadPool pool;
//
std::packaged_task<int(int)> t(f);
std::future r = t.get_future();
pool.post([]{t(5);});
r.get();
//while (1);
getchar();
return 0;
}

@yvoinov
Copy link

yvoinov commented May 19, 2019

You doing it wrong. Use something like that:

std::future<void> r;
std::packaged_task<int()> t([&]() { f(v_input); });
r = t.get_future();
pool.post(t);
r.wait();

PS. It tell you directly - ""t" cannot be caught implicitly because the default capture mode has not been specified". Specify capture mode in lambda.

@mirro187
Copy link
Author

mirro187 commented May 20, 2019

You doing it wrong. Use something like that:

std::future<void> r;
std::packaged_task<int()> t([&]() { f(v_input); });
r = t.get_future();
pool.post(t);
r.wait();

PS. It tell you directly - ""t" cannot be caught implicitly because the default capture mode has not been specified". Specify capture mode in lambda.

Thank you so much for your reply !but, there are still some errors.
how should I modify this code?

Error C2664 "STD: : function < _Ret (void) > : : function (STD: : function < _Ret (void) > &&)" :1 cannot be parameters from the "main: : < lambda_52a96aedc09236acf9c18022602966b7 >" into "STD: : nullptr_t ConsoleApplication4" c: \ program files \ Microsoft visual studio (x86) vc 14.0 \ \ include \ the future 582

#include "thread_pool.hpp"
#include "thread"
#include "future"
#include "functional"
#include "memory"
#include "iostream"
using namespace std;

class cc{
public:
int f(int j)
{
return j;
}
};

int main(int argc, char **argv)
{
tp::ThreadPool pool;
cc obj;
std::future r;
std::packaged_task<int()> t(& {bind(&cc::f,&obj,1);});
r = t.get_future();
pool.post(t);
getchar();
return 0;
}

@yvoinov
Copy link

yvoinov commented May 20, 2019

What would you like to achieve on so complex manner?

To post subroutine call to pool just enough to wrap it in lambda directly:

	pool.post(([=]() { f(j); }));

No future, no bind requires.

Also keep in mind: using future here lead to serialization and makes pool useless.

@mirro187
Copy link
Author

so, there are still some errors.please use the vs2015 run this code to see your result.

**
Error C2664 "STD: : function < _Ret (void) > : : function (STD: : function < _Ret (void) > &&)" :1 cannot be parameters from the "main: : < lambda_52a96aedc09236acf9c18022602966b7 >" into "STD: : nullptr_t ConsoleApplication4" c: \ program files \ Microsoft visual studio (x86) vc 14.0 \ \ include \ the future 582
**

What would you like to achieve on so complex manner?

To post subroutine call to pool just enough to wrap it in lambda directly:

	pool.post(([=]() { f(j); }));

No future, no bind requires.

Also keep in mind: using future here lead to serialization and makes pool useless.

please,

@yvoinov
Copy link

yvoinov commented May 21, 2019

Ah, vs2015. AFAIK this version does not fully C++11 compliant. Try to use more fresh compiler.

Unfortunately, I cant verify due to does not using Windows.

@mirro187
Copy link
Author

Use a lambda to decide how your function is going to be invoked. E.g.

pool.post([]{ t(5); });

Thank you so much for your reply !but, there are still some errors.
how should I modify this code?

Error C2664 "STD: : function < _Ret (void) > : : function (STD: : function < _Ret (void) > &&)" :1 cannot be parameters from the "main: : < lambda_52a96aedc09236acf9c18022602966b7 >" into "STD: : nullptr_t ConsoleApplication4" c: \ program files \ Microsoft visual studio (x86) vc 14.0 \ \ include \ the future 582

#include "thread_pool.hpp"
#include "thread"
#include "future"
#include "functional"
#include "memory"
#include "iostream"
using namespace std;

class cc{
public:
int f(int j)
{
return j;
}
};

int main(int argc, char **argv)
{
tp::ThreadPool pool;
cc obj;
std::future r;
std::packaged_task<int()> t(& {bind(&cc::f,&obj,1);});
r = t.get_future();
pool.post(t);
getchar();
return 0;
}

@yvoinov
Copy link

yvoinov commented May 23, 2019

Your code produces syntax error:

test12.cc: In function 'int main(int, char**)':
test12.cc:21:13: error: missing template arguments before 'r'
std::future r;
^
test12.cc:22:31: error: expected primary-expression before '{' token
std::packaged_task<int()> t(& {bind(&cc::f,&obj,1);});
^
test12.cc:23:1: error: 'r' was not declared in this scope
r = t.get_future();
^
g++: error: test12.o: No such file or directory
g++: fatal error: no input files

To make it work it is enough to write:

#include "thread_pool.hpp"
#include "functional"
#include "memory"
#include "iostream"
using namespace std;

class cc{
public:
int f(int j)
{
return j;
}
};

int main(int argc, char **argv)
{
tp::ThreadPool pool;
cc obj;

pool.post([&] { obj.f(5); } );

getchar();
return 0;
}

@mirro187
Copy link
Author

mirro187 commented May 24, 2019

Your code produces syntax error:

test12.cc: In function 'int main(int, char**)':
test12.cc:21:13: error: missing template arguments before 'r'
std::future r;
^
test12.cc:22:31: error: expected primary-expression before '{' token
std::packaged_task<int()> t(& {bind(&cc::f,&obj,1);});
^
test12.cc:23:1: error: 'r' was not declared in this scope
r = t.get_future();
^
g++: error: test12.o: No such file or directory
g++: fatal error: no input files

To make it work it is enough to write:

#include "thread_pool.hpp"
#include "functional"
#include "memory"
#include "iostream"
using namespace std;

class cc{
public:
int f(int j)
{
return j;
}
};

int main(int argc, char **argv)
{
tp::ThreadPool pool;
cc obj;

pool.post([&] { obj.f(5); } );

getchar();
return 0;
}

Thank you so much for your reply !but, there are still some errors.
how should I modify The following code?

#include "thread_pool.hpp"
#include "thread"
#include "future"
#include "functional"
#include "memory"

struct A {
int b(const int &p)
{
return p;
}
void c(int &i)
{
i = 43;
}
};

int main()
{
A a;
int i = 0;
tp::FixedFunction<int(const int &)> f(std::bind(&A::b, &a, i));
auto f1 = std::move(f);

tp::ThreadPool pool;

std::packaged_task<int(const int &)> t(f1);

std::future<int> r = t.get_future();

pool.post(t);
return 0;

}

@mirro187
Copy link
Author

mirro187 commented Jun 6, 2019

Thank you so much for your reply !but, big brother... I want use std::future method to get thead result value.
so,What should I modify code?
`
#include "thread_pool.hpp"
#include "thread"
#include "future"
#include "functional"
#include "memory"

struct A {
int b(const int &p)
{
return p;
}
void c(int &i)
{
i = 43;
}
};

int main()
{
A a;
int i = 0;
//tp::FixedFunction<int(const int &)> f();
tp::ThreadPool pool;
std::packaged_task<int(const int &)> t(std::bind(&A::b, &a, i));
std::future r = t.get_future();
pool.post(t);
return 0;
}
`

Your code produces syntax error:

test12.cc: In function 'int main(int, char**)':
test12.cc:21:13: error: missing template arguments before 'r'
std::future r;
^
test12.cc:22:31: error: expected primary-expression before '{' token
std::packaged_task<int()> t(& {bind(&cc::f,&obj,1);});
^
test12.cc:23:1: error: 'r' was not declared in this scope
r = t.get_future();
^
g++: error: test12.o: No such file or directory
g++: fatal error: no input files

To make it work it is enough to write:

#include "thread_pool.hpp"
#include "functional"
#include "memory"
#include "iostream"
using namespace std;

class cc{
public:
int f(int j)
{
return j;
}
};

int main(int argc, char **argv)
{
tp::ThreadPool pool;
cc obj;

pool.post([&] { obj.f(5); } );

getchar();
return 0;
}

@yvoinov
Copy link

yvoinov commented Jun 6, 2019

Using future is bad idea here, due to thread pool serialization. With this you pool will execute one task a time.

@mirro187
Copy link
Author

mirro187 commented Jun 8, 2019

Using future is bad idea here, due to thread pool serialization. With this you pool will execute one task a time.

thanks for the warning, But I still want to use std::future method... How should I pass parameters?

@yvoinov
Copy link

yvoinov commented Jun 8, 2019

If you're insist....On your responsibility:

#include "thread_pool.hpp"
#include <functional>
#include <memory>
#include <iostream>
#include <future>

class cc{
public:
static int f(int j)
{
return j;
}
};

int main(int argc, char **argv)
{
tp::ThreadPool pool;
cc obj;

std::packaged_task<int()> t([&]() { return obj.f(5); });
std::future<int> r = t.get_future();
pool.post(t);
r.wait();

getchar();
return 0;
}

This works.

Don't understand, why thread pool here... It will not work as pool. Just enough to use std::async.

But I'm not C/C++ programmer.

@mirro187
Copy link
Author

Thank you, you are a kind one.

If you're insist....On your responsibility:

#include "thread_pool.hpp"
#include <functional>
#include <memory>
#include <iostream>
#include <future>

class cc{
public:
static int f(int j)
{
return j;
}
};

int main(int argc, char **argv)
{
tp::ThreadPool pool;
cc obj;

std::packaged_task<int()> t([&]() { return obj.f(5); });
std::future<int> r = t.get_future();
pool.post(t);
r.wait();

getchar();
return 0;
}

This works.

Don't understand, why thread pool here... It will not work as pool. Just enough to use std::async.

But I'm not C/C++ programmer.

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

3 participants