How to use the multi-thread to make effective use of the remaining cpu resources

I use MainLoop.addThread api in my server code.

But it seems like just using 2 cores

How to use the multi-thread to make effective use of the remaining CPU resources.

How many time do you call addThread? If you only add one then yes it’ll only use 2 cores, if you want to use more you need to add more threads.

I suggest that you focus on what you wish to do and let the operating system mind its own business. :slight_smile: It will dispatch any runnable thread on any core that can run it at that instant. It must deal not only with your application but also every other one that might exist at the same time. If you actually have a workload that is CPU-intensive and that can be profitably and efficiently handled by threads that don’t have to interact with one another – a scenario that is extremely rare – you could maintain a pool of “worker-bee” threads to process them. But it’s also easy to put a lot of “clever thought” into something that really doesn’t pay off like you thought it would.

FYI: A very common and very scalable scenario that is commonly used is to create a fixed, and configurable number of threads which wait on a queue to get work-orders, then send the completed work out by means of another queue. Regardless of the number of work requests that are presently in the queue, the number of active worker-bee threads does not change, and so the number of requests that can be completed per-second remains predictable. Each worker-bee uses mutex facilities in the usual way, but you design each work-request so that they probably won’t actually contend with one another (for long) over any one lock. It’s very much like the way that work is dispatched in a real-world fast food restaurant. The number of workers (and their work assignments) are set according to available resources, and a waiting-line in the front lobby takes care of the rest. (The drive-thru customers are of course more impatient, and are more-literally serialized.)