ddd
ddd

Reputation: 481

Boost synchronous Client and Server - infinite loop blocking the rest

i'm using a synch server and client that reads in an infinite loop.

for (;;){
  boost::system::error_code error;
  read(socket,boost::asio::buffer(&abc, sizeof(abc)));
...
}

what would be the best way to resolve the blocking of the rest of the program since i would like to use snych not asynch. (thread?)

thx in advance..

Upvotes: 0

Views: 442

Answers (1)

bobah
bobah

Reputation: 18864

make it a thread-per-connection with a thread safe queue to serve as a main thread's inbox. It does not scale for multiple connections, but it is the safest and easiest thing to do when you do not need scalability. Writes will be done by the main thread directly into the socket. Reads will be done by the dedicated thread, it will be accumulating read data bits in a temporary buffer till it has received the whole message and then it would forward the full message into main thread's inbox queue. If you need to serve many connections or are limited in resources, then you need to use non-blocking IO with select()/epoll() based event loop (aka reactor).

Upvotes: 1

Related Questions