Multy thread problem

Options
oman
oman
edited August 2012 in DotNet
In Unity3d client, I created a work thread to process the photon's communication, in the thread I called the Peer.Service(), the connection request cann't be sent, what's the problem? or the photon client's data processing must be execute in the main thread? I want to process the network data's sending and receiving in a work thread, I just need call the Peer.Service() in the thread ,right? any other better support for this in photon client api?

Comments

  • Tobias
    Options
    From our side, there is no restriction, which Thread calls Service (or other Photon methods). I'm not sure if Unity limits creation of sockets to the Main thread. I think DNS resolution is locked to the main thread, so maybe using a IP (after doing DNS resolution yourself) is a workaround.

    The latest Photon libraries for Unity are thread safe. Don't expect this to work before Photon3Unity3D.dll v3.0.1.12 though (it wasn't thread safe by design until then).
  • dreamora
    Options
    Nope there is no such limitation in Unity either.
    for raw sockets you would actually be forced to do it if its meant to work reasonably out of my experience (due to the update / fixed update inaccuracy).

    But there is a limitation in Unity at least that forces you to call Service in the main thread at least if its meant to work cleanly cause otherwise the callbacks can not do their job cleanly as NOTHING from an other thread is allowed to access any classes and class instances from UnityEngine.Object descendants. So if you call service in another thread, you are forced in Unity to make callbacks call into direct System.Object extendants that feed 'event queues' which you interpret in MonoBehaviours, you can not call directly to them, which makes it a rather painfull thing. (as a note: Unity is no .NET application, its a C / C++ application that uses Mono / .NET for scripting. Due to this its not threading and async callback safe as the native layer force controls mono, its not self controlled)

    Thats likely the reason why the Unity client has its network manager class which does the service inside a MonoBehaviour and why PUN does the same cause it couldn't invoke its event receivers otherwise.