Is better sync or async from boost asio when there is lot of calculation and push/pop on thread safe containers?

StackOverflow https://stackoverflow.com/questions/23438293

  •  14-07-2023
  •  | 
  •  

Pergunta

Need advice on boost::asio because I am totally new and have deadline soon, I need to create a TCP server (lot of connections) and I used the chat server example from the documentation as a start point.

When I receive a message I have lot of calculation over and I need to push in thread-safe queue (lock guard mutex). Except writing and reading everything calculates in main thread ( where callback executes ?). For this purpose do I need to put synchronous with lot of threads maybe or is there any rule how to make async with lot of calculations quicker ?

(I can put calculation in new async but I wonder is there better solution )

Foi útil?

Solução

Just handle the communication asynchronously, on a single thread. This should allow up to ~10k connections per second. Just don't perform anything slow on this thread. Just push onto the queue and yield to the communication service.

Now, start as many threads as can usefully do the CPU intensive work (usually #of logical core, but sometimes #physical cores and certainly if you are saturating the communication throughput (unlikely), may (#cores - 1)).

If you anticipate that the IO side will be saturated and you cannot afford to block even on the mutex, use a lockfree queue. In that case, definitely dimension (#cores -1) workers, because the workers would naturally spin in a tight loop waiting for messages on the queue, suffocating the IO thread if you don't take precautions.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top