First and foremost, I want to apologize for the lengthy post. I wanted to be as thorough as possible.
I've been stuck on this issue for a few days now, and there is surprisingly little information regarding the proper use of boost::packaged_task
on a function that has input parameters.
System Info
- C++03
- Boost 1.54.0
- CMake 2.8.9
The Initial Requirement
- I have a setup that consists of client(s), a server, and device(s).
- A client interacts with a device by sending requests to the server.
- These requests are examined and routed to the appropriate device.
- Requests are handled asynchronously, and are occasionally queued up via
boost::asio::io_service::strand
for various reasons.
- Requests are placed into a queue, local to the device itself.
- When the request has been acknowledged (not necessarily completed), it is assigned an ID, and returned to the client.
Packaged Task
After looking through boost::futures we decided that boost::packaged_task would do exactly what we need. However, there appears to be a bug in the implementation of packaged task.
It appears as if packaged_task has a few different templates to choose from:
packaged_task<R>
packaged_task<R()>
packaged_task<R(ArgTypes)>
- Others that I may be missing.
To ensure that I was using the function correctly, I started simple; using the simple example on the boost::futures page as a starting point. From there, I created four simple functions:
- int return, no parameters.
- int return, with parameters.
std::string
return, no parameters.std::string
return, with parameters.
Test functions
std::string ans("forty two");
int int_no_params()
{
return 42;
}
int int_with_params(int param)
{
return param;
}
std::string string_no_params()
{
return std::string("forty two");
}
std::string string_with_params(std::string & param) // Have tried both with and without '&'
{
return param;
}
EXAMPLE 1:
int function(void)
//! Compiles and produces correct result.
{
boost::packaged_task<int()> example(int_no_params);
boost::future<int> f = example.get_future();
boost::thread task(boost::move(example));
int answer = f.get();
std::cout << "Answer to life and whatnot, in English: " << answer << std::endl;
task.join();
}
EXAMPLE 2:
std::string function(void)
//! Compiles and produces correct result.
{
boost::packaged_task<std::string()> example(string_no_params);
boost::future<std::string> f = example.get_future();
boost::thread task(boost::move(example));
std::string answer = f.get();
std::cout << "string_no_params: " << answer << std::endl;
task.join();
}
EXAMPLE 3:
std::string(std::string& param)
No threading
//! Doesn't compile.
//! error: variable ‘boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)> example’ has initializer but incomplete type
{
boost::packaged_task<std::string(std::string&)> example(string_with_params);
boost::future<std::string> f = example.get_future();
example(ans);
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
}
EXAMPLE 4:
using boost::threading
//! Doesn't compile.
//! error: variable ‘boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)> example’ has initializer but incomplete type
{
boost::packaged_task<std::string(std::string&)> example(string_with_params);
boost::future<std::string> f = example.get_future();
boost::thread task(boost::move(example), ans);
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
task.join();
}
EXAMPLE 5:
Using extended initializers in packaged_task declaration
//! Doesn't compile in C++03, C++11 only.
//! error: extended initializer lists only available with -std=c++11 or -std=gnu++11 [-Werror]
{
boost::packaged_task<std::string(std::string&)> example
{ boost::bind(&string_with_params, ans) };
boost::future<std::string> f = example.get_future();
boost::thread task(boost::move(example), ans);
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
task.join();
}
EXAMPLE 6:
Threaded, using shared_ptr
The following use
typedef boost::packaged_task<std::string(std::string&)> task_t;
Because packaged tasks can't be copied, binding shared_ptr<T>::operator()
to task
was a suggested solution found here.
// error: invalid use of incomplete type ‘class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>’
// error: incomplete type ‘task_t {aka boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>}’ used in nested name specifier
// boost/thread/future.hpp:1320:11: error: declaration of ‘class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>’
{
boost::shared_ptr<task_t> example = boost::make_shared<task_t>(boost::bind(&string_with_params, ans));
boost::future<std::string> f = example->get_future();
boost::thread task(boost::bind(&task_t::operator(), example));
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
task.join();
}
EXAMPLE 7:
Using boost::asio::io_service
and boost::bind
// error: invalid use of incomplete type ‘class boost::packaged_task(std::basic_string&)>’ // error: incomplete type ‘task_t {aka boost::packaged_task(std::basic_string&)>}’ used in nested name specifier // boost/thread/future.hpp:1320:11: error: declaration of ‘class boost::packaged_task(std::basic_string&)>’
{
boost::asio::io_service io_service;
boost::thread_group threads;
boost::asio::io_service::work work(io_service);
for (int i = 0; i < 3; ++i)
{
threads.create_thread(boost::bind(&boost::asio::io_service::run,
&io_service));
}
boost::shared_ptr<task_t> example = boost::make_shared<task_t>(boost::bind(&string_with_params, ans));
boost::future<std::string> f = example->get_future();
io_service.post(boost::bind(&task_t::operator(), example));
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
threads.join_all();
}
Is there something I am doing awfully wrong here? I feel like I've exhaustively tested this and haven't made any headway. I have tried every other combination of binds, threads, and tasks to get this working, but it's simply not happening. I appreciate any help you provide.
As a final note:
I have a working solution using futures and promises, and by using a private function to post to my thread, I return a valid future. This issue just seems to be something that isn't necessarily user error.
Thanks for reading.