samedi 31 octobre 2015

Elegantly stop the thread for blocking or non-blocking when accept or receive using asio and C++11

I am using asio for tcp server and I plan to use C++11 std for thread as well. My final goal is to have this application on Linux but I am testing it on Windows first using Visual Studio 2015.

First I am using blocking so I found there were discussion on how to stop a thread waiting on accept. There were pre-C++11 solutions such as pipe, and select. Is there a better way to do this now with C++11 and asio?

Second way is to use asynchronous, but I prefer to use the asio header only so what is the complete solution using std::bind? I couldn't find the std equivalent of this bytes_transferred. The compile complained that the asio::placeholder doesn't have this member.

    asio::async_write(socket_, asio::buffer(message_),
        std::bind(&tcp_connection::handle_write, shared_from_this(),
            std::placeholders::_1,
            asio::placeholders::bytes_transferred// complain here
            ));

The third way is, to set a timeout here. But it doesn't stop my accept. Plus, the answer was in 2012. I hope there is new was to solve this by now.

Aucun commentaire:

Enregistrer un commentaire