@@ -27,6 +27,11 @@ void Messenger::CleanUp() {
2727 publisher_.close ();
2828 subscriber_.close ();
2929 context_.close ();
30+ for (auto & t : sub_threads_) {
31+ if (t.joinable ()) {
32+ t.join ();
33+ }
34+ }
3035 } catch (...) {
3136 // 忽略析构期异常
3237 }
@@ -55,7 +60,8 @@ std::string Messenger::GetPubEndpoint() const { return endpoint_; }
5560void Messenger::Sub (const std::string& topic, const std::function<void (const std::string&)>& callback) {
5661 sub_threads_.emplace_back ([=, this ]() {
5762 try {
58- zmq::socket_t subscriber (context_, zmq::socket_type::sub);
63+ zmq::context_t context = zmq::context_t (1 );
64+ zmq::socket_t subscriber (context, zmq::socket_type::sub);
5965 subscriber.connect (endpoint_);
6066 subscriber.set (zmq::sockopt::subscribe, topic);
6167
@@ -81,7 +87,8 @@ void Messenger::Sub(const std::string& topic, const std::function<void(const std
8187void Messenger::SubStruct (const std::string& topic, const std::function<void (const void *, size_t )>& callback) {
8288 sub_threads_.emplace_back ([=, this ]() {
8389 try {
84- zmq::socket_t subscriber (context_, zmq::socket_type::sub);
90+ zmq::context_t context = zmq::context_t (1 );
91+ zmq::socket_t subscriber (context, zmq::socket_type::sub);
8592 subscriber.connect (endpoint_);
8693 subscriber.set (zmq::sockopt::subscribe, topic);
8794
0 commit comments