I was trying the differents tutorials in Boost.Asio documentation and tried to replace boost components with C++11 ones. However, I got an error using std::bind in Timer.5 - Synchronising handlers in multithreaded programs. Here is the code proposed:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer { /* Not relevent here */ };
int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}
I tried to replace boost::thread
by std::thread
and boost::bind
by std::bind
. Here is my code:
#include <functional>
#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer { /* Not relevent here */ };
int main() {
boost::asio::io_service io;
printer p(io);
std::thread t(std::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
}
When compiling with GCC 4.7, I got this compile-time error:
g++ -std=c++0x main.cpp -lboost_system -lboost_date_time -lpthread
main.cpp: In function ‘int main()’:
main.cpp:52:60: erreur: no matching function for call to ‘bind(<unresolved overloaded function type>, boost::asio::io_service*)’
main.cpp:52:60: note: candidates are:
/usr/include/c++/4.6/functional:1444:5: note: template<class _Functor, class ... _ArgTypes> typename std::_Bind_helper::type std::bind(_Functor&&, _ArgTypes&& ...)
/usr/include/c++/4.6/functional:1471:5: note: template<class _Result, class _Functor, class ... _ArgTypes> typename std::_Bindres_helper::type std::bind(_Functor&&, _ArgTypes&& ...)
Where is this error comming from, taking into account that I did not use any boost::asio::placeholders
(as explain in this stackoverflow question Should std::bind be compatible with boost::asio?)?
std::bind
for you, e.g.,std::thread t([&io]() { io.run(); });
. This avoid the overload resolution entirely. – Cristinecristiona