有很多太少代碼,但在這裏有雲:
#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