c++ - I have a server listening on sockets, whats a good approach to service CPU-bound requests with multiple threads? -


I have an application written in C ++, which encourages: uses ASIO requests on this socket Hears and does not have some CPU-bound work (for example, a disk or network I / O) for each request, and then responds with feedback.

This application is a multi-core system, so I plan to process (at least) 1 thread per core in order to process requests in parallel.

What's the best way to do this? Things to think about:

  • I'll need a fixed size thread pool (i.e. 1 thread per CPU)
  • If I have more requests than Threads Then they

the server is currently single threaded:

  • This client waits for the request
  • Receiving the request once After this, it works, and writes back the response, then begins waiting for the next request

Update:

More specifically: I should use the mechanism to make sure that incoming requests are queued if the server is busy is? Which mechanism should I use to distribute the incoming requests between N Thread (1 per core)?

I don t if you are actually CPU-bound, then adding more threads than the number of cores will help you Do not except If you are going to request a lot of requests, the hearing queue can not meet your needs or may have some threads to accept the connection and yourself Better to be queued.

UPDATE:

Listen () is the second parameter that is in the depth of your requested OS / TCP queue. You can set it up to the OS limit Also you need to play with the system knob. On my current system it is 128, so it is not big, but not trivial. Check your system and think whether you really want something bigger than the default.

There are several directions from beyond that you can go. Think about kissing - there is no complexity before it is really necessary. Start in a few simple ways, to accept a connection to a thread bus (to some extent) and to knock them in the queue. The worker thread selects them, processes them, writes out the results, and removes the socket around.

The current speed of my disaster boost update (and I am lacking to compress it) ASIO - so I can not help with it.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -