Logic?

05/30/2016 13:07 iBotx#1
Hello guys,
I have a question regarding threads.
Shall i use a thread for each player? Like : stamina, flags, escorts shit like this.
Normal threads (1 for the whole server) : Monsters, Tournaments.

Opinions and why?
Thanks in advance.
05/30/2016 19:44 Spirited#2
In most cases, a thread-per-session model isn't going to work for a Conquer Online server (unless you plan on only hosting a handful of people). A thread is a separate path of execution that needs computation time on your processor. The more threads you have, the more processing time is split up and the more time taken in switching between those thread contexts. I'm not saying you can't overbook and have more threads than your processor cores, but one thread per player isn't going to stale; especially on a Raspberry Pi, if that's still on the table.
05/30/2016 20:23 pro4never#3
The more common setup I've seen is using a handful of worker threads and queueing work to thread safe collections to be processed.

Async sockets are going to be using a background threadpool to handle their data. You can then queue up waiting data to be processed. Often the work of processing will be split into a handful of worker threads to minimize context switching and to try to keep everything that could conflict on the same thread.


For example... lets say you have a thread that handles all database logic, it makes sense that when you go to save a value (lets say saving a character when they log out) to switch all of the work to that single database thread. it ensures that (hopefully) you avoid race conditions and you don't lock up a separate thread that might have other work waiting.

The same can be done for things like all entity updates (status effects, timed actions, etc), for monster background AI (NOT triggering the actual actions, just planning it out), for all battle or map functions, for packet processing or for a handful of other things.

This doesn't mean you don't have to take into account thread safety in your collections but you can at least try to limit the number of times that threads could be conflicting with eachother and try to split the work into manageable sections.

I'm not super familiar with how well thread priorities work but it's something you might want to look into as well. Things like saving to database, planning future monster movement paths and other stuff like that don't need to be as high priority as say... processing a player movement packet. By having them split to their own worker thread you may have the possibility of shifting around their priority (I'm guessing though that it may be best to leave that up to the OS to manage)