Multithreading Question

Options
OmegaGames
edited January 2014 in Photon Server
Hi, I have a question on PhotonServer's PeerBase class and multithreading. Is it safe to start a short-lifetime thread that will access the send event and disconnect methods of the PeerBase without a lock or am I required to use the lock below. What sort of issues will I run into for the short duration of the lock and is it possible I could deadlock with something under the hood?

Any information on how I should go about starting this thread would be great. I don't want to halt execution by Sleeping on the main thread. The reason I'm sleeping though is if I don't sleep it seems to disconnect me before my client can receive the event. This may be because I stop polling the listener for events after disconnection; I don't really want to rewrite that client side.

So is there any other way to handle this, is this code below the recommended or do I not need to lock at all?

More information: This thread is being created and started in the constructor for a PeerBase instance.

[code2=csharp]private void HandleNoLoginServer()
{
//'this' is an instance of PeerBase
lock (this)
{
//Send an event
//Send another event
}
Thread.Sleep(5000);
lock (this)
{
this.Disconnect();
}
}[/code2]

Comments

  • chvetsov
    Options
    Hi, OmegaGames

    it is public interface of PeerBase is thread-safe, you may use it from different threads
  • OmegaGames
    Options
    Thank you for the answer.

    This means I do not need to lock and, for future reference, don't need to worry about asynchronously calling SendEvent(...) and etc at the same time from different threads causing problems?
  • chvetsov
    Options
    yes, you do not need to lock and do not need to worry