There is a Web server, I want to add clients to streams, but limit their number (for example, no more than 10 streams).

Hence the task: how does a new client come in, create a thread for it, if all threads are busy, then expect the release of any stream.

Now it only occurs to me to create a container (I have not decided on which one to choose) and insert new threads into it, then launch them.
If there are more than 10 threads, then expect the release of one of them.

I also wanted to ask: is it better to kill and create streams (pass a link to this stream to the stream method itself so that it terminates itself and deletes it from the container) or keep them always alive?

  • 3
    Always alive. You need a thread pool implementation, surely in the same boost there is a ready one. - etki
  • I'm not sure that the boost was accepted, but on the net, designed for boost, there is a library just with the thread pool implementation. - Arkady

1 answer 1

It makes no sense to create / kill streams, since it only has problems (portability, speed, handling of creation errors, etc.) and does not bear any profit.

In any situation, whatever it may be, you can estimate how much memory / processor / other resources you are really ready to “sacrifice” for processing clients. Let's say you are ready to donate resources to 10 threads. Then it will be optimal to launch 10 threads at once, which will work when there is work, and sleep when there isn’t.

Everything further (what to do with the new client, how to handle existing ones) already depends on the architecture of your server.

  • Does it make sense to set the number of threads more than the processor cores? - Fangog
  • @Fangog, under certain circumstances - yes. What exactly this circumstances, a question not absolutely trivial for discussion in comments. A simple example: if the code of your threads can sometimes perform long heavy synchronous operations, blocking in these operations, then a blunt increase in the number of threads will speed up such a system. - o2gy