The boost :: asio :: io_service :: call is made from std :: thread

I have a class that processes my connection that has a boost :: asio :: io_service element. I want to call io_service :: run () from a std :: stream, but I am facing compilation errors.

std::thread run_thread(&boost::asio::io_service, std::ref(m_io_service));

Does not work. I see various examples for this using boost :: thread, but I want to stick with std :: thread for this. Any suggestions?

thank

+4
source share
4 answers

There are two ways, as I knew, one of them is to create std :: thread by lambda.

std::thread run_thread([&]{ m_io_service.run(); });

Another is to create boost :: thread with boost :: bind

boost::thread run_thread(boost::bind(&boost::asio::io_service::run, boost::ref(m_io_service)));
+11
source

@cbel. , ( - ) boost:: thread lambdas:

std::thread run_thread(
    std::bind(static_cast<size_t(boost::asio::io_service::*)()>(
        &boost::asio::io_service::run), std::ref(m_io_service)));
+1

io_context.run () can be used instead of io_service.run ().

0
source

For me, both of the answer choices marked as solution led to exceptions.

What it was for me:

boost::thread run_thread([&] { m_io_service.run(); });

instead

std::thread run_thread([&]{ m_io_service.run(); });
0
source

Source: https://habr.com/ru/post/1543802/


All Articles