BewareMyPower commented on a change in pull request #12427:
URL: https://github.com/apache/pulsar/pull/12427#discussion_r732946602



##########
File path: pulsar-client-cpp/lib/ExecutorService.cc
##########
@@ -27,22 +27,30 @@ DECLARE_LOG_OBJECT()
 
 namespace pulsar {
 
-ExecutorService::ExecutorService()
-    : io_service_(new boost::asio::io_service()),
-      work_(new BackgroundWork(*io_service_)),
-      worker_(std::bind(&ExecutorService::startWorker, this, io_service_)) {}
+ExecutorService::ExecutorService() {}
 
 ExecutorService::~ExecutorService() { close(); }
 
-void ExecutorService::startWorker(std::shared_ptr<boost::asio::io_service> 
io_service) { io_service_->run(); }
+void ExecutorService::start() {
+    auto self = shared_from_this();
+    std::thread t{[self] {
+        if (self->isClosed()) {
+            return;
+        }
+        boost::system::error_code ec;
+        self->getIOService().run(ec);

Review comment:
       Yeah. `io_service::work` is only to prevent `run` from returning 
immediately if there's no event. When `io_service::stop` is called, 
`io_service::run` will return immediately. See [Boost 1.53 
io_service::stop](https://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/io_service/stop.html)
   
   > All invocations of its run() or run_one() member functions should return 
as soon as possible. 
   
   Here's an example:
   
   ```c++
   #include <boost/asio.hpp>
   
   #include <ctime>
   #include <chrono>
   #include <iostream>
   #include <thread>
   using namespace std;
   
   int main(int argc, char *argv[]) {
     boost::asio::io_context context;
     boost::asio::io_context::work work{context};
   
     std::thread t{[&context] {
       boost::system::error_code ec;
       context.run(ec);
       printf("%ld io_context run: %s\n", time(nullptr), ec.message().c_str());
     }};
     t.detach();
   
     std::this_thread::sleep_for(std::chrono::milliseconds(500));
     context.stop();
     printf("%ld context is stopped\n", time(nullptr));
     return 0;
   }
   ```
   
   The output:
   
   ```
   1634746880 context is stopped
   1634746880 io_context run: Undefined error: 0
   ```
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to