Forum Multiplayer Thread Locking Model

Discussion and help relating to the PlayerIO Multiplayer API.

Thread Locking Model

Postby victor » May 3rd, 2019, 12:06 am

What is the threading model for PlayerIO? Is it like Node.JS with cooperative scheduling (where locks are usually unneeded), or like other multi-threaded applications (where locks are needed)?

Other posts say that PlayerIO is the latter and that multiple methods in the Game<P> class can be executing simultaneously.

If that is true, does it mean overridden methods (GotMessage, UserJoined, AllowUserJoin, etc.) need to use locks?

Also, doesn't that imply it is unsafe to use a foreach loop over IEnumerable<P> Players, as it could change in the middle of the loop?
victor
 
Posts: 5
Joined: October 9th, 2017, 4:42 am

Re: Thread Locking Model

Postby robscherer123 » May 3rd, 2019, 3:42 pm

Yes the PlayerIO server is heavily multi-threaded. From my experience yes, GotMessage, UserJoined, and all the like can all happen simultaneously. The Players array seems to be threadsafe to iterate through though, at least from my years of experience. If you use Lists or arrays or such that are shared between those functions though, they will certainly NOT be threadsafe. A great solution to that though is to use classes from the System.Collections.Concurrency namespace, such as a ConcurrentDictionary. Using these is alot easier and better performing than placing locks{} everywhere.
robscherer123
Paid Member
 
Posts: 313
Joined: December 12th, 2012, 8:46 pm

Re: Thread Locking Model

Postby victor » May 11th, 2019, 9:43 pm

If Players is guaranteed to be threadsafe when using foreach, is this in the documentation somewhere? I didn't see anything guaranteeing it.

Also, could PlayerCount change while iterating over Players?
victor
 
Posts: 5
Joined: October 9th, 2017, 4:42 am


Return to Multiplayer



cron