Forum C# How much Threads handle Sheduled tasks/Timers?

How much Threads handle Sheduled tasks/Timers?

Postby crotery » September 19th, 2012, 10:09 pm

We have question about production server architecture.

Every client has it's own Thread. That's ok. All message handlers should be thread safe, because clients can send packets anytime, and code will handle them immediately.

But what about Room and this code?

Code: Select all
   AddTimer(tick, 50);   


If I add more "AddTimer" with other functions and delays...or my tick will work more than 50ms...will other Thread tick() simultaneously? Do you have a thread pool, and is there any possibility, that my tick() will execute simultaneously by two threads?

I ask, because I want to know, whether I should make tick() thread safe or not.
crotery
 
Posts: 4
Joined: August 22nd, 2012, 12:00 pm

Re: How much Threads handle Sheduled tasks/Timers?

Postby dreamora » September 20th, 2012, 7:42 am

see below
Last edited by dreamora on September 20th, 2012, 11:57 am, edited 1 time in total.
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am

Re: How much Threads handle Sheduled tasks/Timers?

Postby crotery » September 20th, 2012, 8:45 am

We made some investigation...

Аnd the investigation result tells us, that there is a thread pool, because we observed simultaneous calls to tick() with only one task: AddTimer(tick,50).

This fact explains all problems with concurrent access to Lists,Dictionaries and Arrays: 1 room out of 1000 is crashing sometimes.
If, for example, one tick() adds new element to List, and other tick() enumerate same List (actually it is same function).
crotery
 
Posts: 4
Joined: August 22nd, 2012, 12:00 pm

Re: How much Threads handle Sheduled tasks/Timers?

Postby dreamora » September 20th, 2012, 11:56 am

Assuming thats true (will need to test it on the live server first, dev server is useless for that), that would be very unpleasant to hear about.
On one hand they are paranoid about statics but on the other hand the timer is implemented with thread pools instead of coroutines allowing games to simply blow up or deadlock?!
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am

Re: How much Threads handle Sheduled tasks/Timers?

Postby crotery » September 20th, 2012, 12:34 pm

Timers are concurrent for performance, I think.
If we need simple game logic, we will use SheduledTasks as described here http://playerio.com/forum/csharp/multithreading-issue-what-to-lock-t34126. I think it is linear, guaranteed.
crotery
 
Posts: 4
Joined: August 22nd, 2012, 12:00 pm

Re: How much Threads handle Sheduled tasks/Timers?

Postby Henrik » September 20th, 2012, 3:34 pm

crotery wrote:I ask, because I want to know, whether I should make tick() thread safe or not.

All timers, scheduled callbacks, and API callbacks are independent of each other and can execute simultaneously. So yes, all your code needs to be threadsafe.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How much Threads handle Sheduled tasks/Timers?

Postby Henrik » September 20th, 2012, 3:45 pm

crotery wrote:If we need simple game logic, we will use SheduledTasks as described here http://playerio.com/forum/csharp/multithreading-issue-what-to-lock-t34126. I think it is linear, guaranteed.

Yes, if you use that method you won't get overlapping execution, for obvious reasons. :-)
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How much Threads handle Sheduled tasks/Timers?

Postby numagames » October 28th, 2012, 7:47 am

Henrik wrote:
crotery wrote:I ask, because I want to know, whether I should make tick() thread safe or not.

All timers, scheduled callbacks, and API callbacks are independent of each other and can execute simultaneously. So yes, all your code needs to be threadsafe.


Please can someone explain it more clear for dumb beginner.
I have realtime shooter and server logic is divided into Game's methods's:

UserJoined
UserLeft
GotMessage
Update (which is called by AddTimer or AddScheduledCallback)

If all this methods can be executed simultaneously means I can't manage shared game data in this methods - Players, Bullets, Obstacles Lists? Is the only way to have all game logic in Update method? In Flash AVM where I cam from there is no multithreading and everything is much more stable, please explain what are the usual ways to organize server logic here. Thanks.
numagames
 
Posts: 4
Joined: August 27th, 2012, 6:04 pm

Re: How much Threads handle Sheduled tasks/Timers?

Postby Benjaminsen » October 29th, 2012, 2:58 pm

For performance reasons the Player.IO server is heavily multithreaded, preventing one game from interrupting other games or itself.

You can prevent data issues by using the C# lock statement http://msdn.microsoft.com/en-us/library ... cz(v=vs.71).aspx

We are aware that many developers find this a daunting task and are investigating how to provide a simpler server for those.
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: How much Threads handle Sheduled tasks/Timers?

Postby crotery » October 29th, 2012, 3:15 pm

Unfortunately ConcurrentCollections are denied, so had to write our own to make available readable data safely and locking only while writing.

Also we've used SheduledCallback to make sure we have only one MAIN thread for simplicity, and this thread locks from other ("packet recieve" threads)
crotery
 
Posts: 4
Joined: August 22nd, 2012, 12:00 pm

Re: How much Threads handle Sheduled tasks/Timers?

Postby numagames » October 29th, 2012, 4:09 pm

As I understood, as for the moment the most reliable and secure way to organize code is to do everything in one method, let's call it Update. At the end of the Update's execution we call it again through SheduledCallback. When we get some messages from client through UserJoined, UserLeft or GotMessage we execute nothing there, just setting some flags for Update method that some messages came from client or players joined or left game. This way we are in full control that code executes linearly. Or am I missing something?
numagames
 
Posts: 4
Joined: August 27th, 2012, 6:04 pm

Re: How much Threads handle Sheduled tasks/Timers?

Postby olgeorge » January 30th, 2013, 7:24 pm

numagames wrote:As I understood, as for the moment the most reliable and secure way to organize code is to do everything in one method, let's call it Update. At the end of the Update's execution we call it again through SheduledCallback. When we get some messages from client through UserJoined, UserLeft or GotMessage we execute nothing there, just setting some flags for Update method that some messages came from client or players joined or left game. This way we are in full control that code executes linearly. Or am I missing something?


Same questions here!
olgeorge
 
Posts: 12
Joined: November 9th, 2012, 9:20 am

Re: How much Threads handle Sheduled tasks/Timers?

Postby Henrik » January 30th, 2013, 8:18 pm

olgeorge wrote:Same questions here!

Yes, that suggestions seems ok, if you absolutely must force your game logic through a single thread.

Just remember to lock all structures you use, and to minimize the time spent inside locks. The easiest way is to have some sort of list of events to process. In GotMessage/UserJoined/etc, you lock the list, add an event, and release the lock. And in your Update method, the first thing you do is to lock that list, make a copy, clear the list, and release the lock. And then continue processing your copy, because that then contains the events you need to process this tick, and the shared list may be updated by GotMessage, etc.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm


Return to C#



cron