Forum C# Player Io's timer multithreads?

Player Io's timer multithreads?

Postby GMulligan » February 25th, 2010, 5:42 am

I use a player.io game timer like this: AddTimer(Update, 1000);
and use player.ios public override void GotMessage(Player player, Message m){....} to process messages

Update is capable of deleting objects if they've become too old as well as creating new ones. Meanwhile GotMessage depends on the state of these same objects and is also capable of deleting/adding new ones. I'd just assumed that player.io would be single threaded server side no matter what so during GotMessage my update function would stop running until it was through. However, I was getting null object reference errors while debugging GotMessage so I set breakpoints inside both it and Update and found that Update was still getting called even while I was in the middle of GotMessage.

What's the proper way to handle this? Should I start using locks? Although it occurs to me that then I'd be pausing Update so if I wanted an object to disappear after 10 seconds it wouldnt be precise. Should I stop judging lifespan by the number of times Update has been called and instead use DateTime?
GMulligan
 
Posts: 8
Joined: February 1st, 2010, 4:02 am

Re: Player Io's timer multithreads?

Postby Oliver » February 25th, 2010, 1:21 pm

Hi GMuligan,

All player.io code is *currently* multithreaded by default. As you've noticed, this means that an update method can fire while a GotMessage method is running.

Currently, if you want to avoid this behavior, you'll have to use lock(obj){...} to synchronize your threads; however, given the very low, low level of concurrency (a few people in the same room sending messages), most code will probably execute as if in a single threaded environment -- i.e, you saw the issue because you were debugging, multithreading probably didn't cause the issue you were debugging in the first place.

We're considering adding a feature that will switch the default to single-threaded, such that only one piece of gamecode will run at a time, and then perhaps make multithreaded an option.

What's the proper way to handle this? Should I start using locks? Although it occurs to me that then I'd be pausing Update so if I wanted an object to disappear after 10 seconds it wouldnt be precise. Should I stop judging lifespan by the number of times Update has been called and instead use DateTime?


You're right that it wouldn't be precise, but the code is executing so fast anyways, that unless you're doing a heck-of-a-lot of work in the update method, you won't notice.

If you want really exact timespans, set a variable to DateTime.Now when the game starts and use timepassed=DateTime.Now-startTime to get the difference.
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am


Return to C#



cron