Protecting my application from race conditions

Options
jaja1
jaja1
edited December 2014 in Photon Server
Right now I am battling with a very confusing problem on my server. I have integrated a physics engine and I believe photon server is causing race conditions when I try to manipulate my spaces and the objects within them, though I am not 100% certain of this.

I am quite new to the concept of a multithreaded coding and I have never had this problem until now. How can I determine for sure that my application is being affected by a race condition and not a logical error in my code, and if it is a race condition how can I prevent them at various stages of execution in my code (ie. where exactly should I be using volatile statements or locks/memory barriers?). Is there any detailed documentation that relates specifically to photon server sdk and preventing this sort of thing from happening?

Thanks in advance for any help :)

Comments

  • chvetsov
    Options
    Hi, jaja1
    As you understand this forum is not about multithreaded coding. And it is imposible in one or to replies explain everything about it. so i leave this task for you. you should take right book or find something in internet about it.

    Here in Photon we did as much as possible to simplify things. How do we do this?
    1. you may use PhotonPeer methods from any thread.
    2. all notifications from PhotonPeer are executed in one fiber (read about this below)
    3. all task in room executed in one fiber.
    4. Peers send messages to room's fibers in order to protect data from multithreading issues

    What means fiber? Fiber is a list of tasks which are executed one by one sequntionaly in FIFO manner. This does not mean that they are executed in one thread, actually they are executed in many threads, but one by one. So first task may be executed in thread A, when first task is finished second task may be executed in thread B. and so one. But at every given moment just ONE thread accesses rooms data. this should be clear.
    So once again: many threads have access to rooms data, but at any given moment just ONE
    In cases when many fibers access some data, we use locks. For instance room's cache

    Also, it is not good idea, to start long processing in response to room message. You should do this in dedicated thread.
    so, if you use some physical engine, you should start work with it in dedicated thread. When you got snapshot you may send it to room with message by calling room's EnqueueMessage method. if you need to calculate some collsions you also shold send some request to physics thread, and response will be returned bach through message.
    Without exact knowlege of what you are going to create it is difficult to give you any recomendations. But may be you could use something like this.
    // rooms contructor
    physicsFiber = new PoolFiber();// create new fiber for Physics
    physicsFiber.Start();
    physicsFiber.ScheduleForInterval(this.CalcualteWorld, 0, 100); // start immediately and repeat every 100 ms, ie 10 times per second. this params may vary as you need

    // at some other point, where you need to calculate collsions
    physicsFiber.Enqueu(()=>Collide(here all parameters you needed for calcualation and to find who made this request));

    // at the end of Collide member
    room.EnqueueMessage(new YourMessage(results of calucation, data to find who made request))

    I hope this will help you
  • jaja1
    Options
    Thank you so much @chvetsov !!! Your explanation really helped me understand how Photon's multithreading processes work and your pseudo code fit my needs perfectly. I rewrote some major parts of my server and it is proving so far to be better than what I was previously doing. :D
  • chvetsov
    Options
    Excellent! I'm glad to hear this.
    Happy New Year