First of all, I want to apologize for the long post. I wanted to be as thorough as possible.
I have been stuck with this problem for several days, and there is surprisingly little information about the proper use boost::packaged_taskfor a function that has input parameters.
System Information
- C ++ 03
- Boost 1.54.0
- CMake 2.8.9
Initial requirement
- I have a setup consisting of clients, server and devices.
- The client interacts with the device by sending requests to the server.
- These requests are checked and sent to the appropriate device.
- Requests are processed asynchronously, and they are sometimes queued through
boost::asio::io_service::strandfor various reasons.
- , .
boost:: futures , boost:: packaged_task , . , , .
, packaged_task :
packaged_task<R>packaged_task<R()>packaged_task<R(ArgTypes)>- , .
, ; boost:: futures . :
- int return, .
- int return, .
std::string return, .std::string return, .
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)
{
return param;
}
1:
int function(void)
{
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();
}
2:
std::string function(void)
{
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();
}
3:
std::string(std::string& param)
{
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;
}
4:
boost:: threading
{
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();
}
5:
packaged_task
{
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();
}
6:
, shared_ptr
typedef boost::packaged_task<std::string(std::string&)> task_t;
, shared_ptr<T>::operator() task .
{
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();
}
7:
boost::asio::io_service boost::bind
//: . class boost:: packaged_task (std:: basic_string &) > //error: incomplete type 'task_t {aka boost:: packaged_task (std:: basic_string &) > } //boost/thread/future.hpp: 1320: 11: : 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();
}
-, ? , . , , , . , .
:
promises, . , , -, .
.