Photon SDK multithreading

I am very new to multi threading, so please bare with me... (please correct me if I say something wrong, I am writing how I understand it)

I am building server side game logic using Photon SDK LiteApplication.

From what I understand, every peer runs in its own thread... also every room has its own thread via ThreadFiber API (?)

I have a World object on the server, which is not tied to any specific room, but I need to tick it, so I create thread fiber there and do something like this:

ThreadFiber fiber = new ThreadFiber("Worker Thread");
fiber.ScheduleOnInterval(Tick, 50, 50);

I believe that would also execute on a different thread(?)

How can I ensure thread safety, i,e, if World ticks and updates some data, it does not cause problem if some peer or a room is also trying to read/write data on the same data at the same time?

Is there any API within Photon to facilitate this, or do I need to pretty much place "lock" statements in every function?

Thank you for help.

Comments

  • hi, @cpgames

    you are not really right in your assumptions. Peers and Rooms use PoolFibers that do not use thread, but the System Thread Pool.
    if you have only one world for many rooms than it is ok to use ThreadFiber and yes, you may tick it way you wrote above

    in order to keep thread safety you should stick to idea that only one fiber may access data at time. Does not matter what type of fiber you have. It should be only one. If you have two fiber (for instance peer and room) then you have two options:
    - use Enqueue method of fiber
    - use lock's

    so, your world may enqueue some events to rooms. Rooms may enqueue some request to world's fiber and get response using some callbacks and so on.
    In some cases when you do not have fiber, you have to use locks

    best,
    ilya
  • Thanks...I think I got that working. But now if I debug in VS and open threads window, every tick shows up with a different threadId, even though it was queued onto the same fiber. Is this correct? (sorry again for confusion, I am new to this)
  • yes, that is correct. It is possible that every enqueued action will be executed in different thread. the only promise is kept that non of the actions executed at same time, always one by one

    best,
    ilya
  • cpgames
    cpgames
    edited May 2020
    Thanks Ilya.

    And what happens when I call Join() on a ThreadFiber which has a Schedule setup?

    So for example, I have World.Fiber which executes a tick every second. I want to enqueue an additional action there and wait until its complete to do things on another fiber/thread.
    e.g.

    World.Fiber.Schedule(World.Tick(), 0, 1000);
    ....
    Result r;
    World.Fiber.Enqueue(() => { r = World.CalculateResultt(); });
    World.Fiber.Join();
    UseResult(r);

    Will this work, and where would the next World.Tick() execute? Will it deadlock the calling thread because its going to Tick forever?
  • well, I'm not so experienced with ThreadFiber. We use mostly PoolFiber

    but I would say that Join is usually used to find out when thread finished. ThreadFiber runs forever. it only gets new actions. if you need to know when some action finished you either should call callback after CalculateResults() or set event to signaled state.

    In case of event you have to understand that waiting thread is blocked

    best,
    ilya