有很多太少代码,但在这里有云:
#include <boost/asio.hpp>
#include <boost/optional.hpp>
#include <boost/thread.hpp>
#include <iostream>
using namespace boost::asio;
struct asio_object {
protected:
mutable io_service io_service_;
private:
boost::optional<io_service::work> work_ { io_service::work(io_service_) };
boost::thread th { [&]{ io_service_.run(); } };
protected:
asio_object() = default;
~asio_object() { work_.reset(); th.join(); }
};
struct Client : asio_object {
public:
void set_message(std::string data) {
io_service_.post([=]{
message = data;
std::cout << "Debug: message has been set to '" << message << "'\n";
});
}
private:
std::string message;
};
struct Server : asio_object {
Client& client_;
Server(Client& client) : client_(client) {}
void tell_client(std::string message) const {
client_.set_message(message);
}
};
int main()
{
Client client;
Server server(client);
server.tell_client("Hello world");
}
(这是一个有点胡乱猜测的,因为你没有准确地描述在具体条款的问题)
这是非常很难弄清楚你想要什么。我抨击了一些示例代码。如果没有回答,请使用它为您的问题创建SSCCE。 – sehe 2014-11-08 21:20:21
这是一个非常模糊的问题。通常,使用多个'io_service'对象的应用程序可以通过[发布工作](http://www.boost.org/doc/libs/1_55_0/doc/html/booster_asio/reference/io_service/post.html)相应的io_service。 – 2014-11-10 04:36:31