How to check if a thread I'm on is a Unity thread?
I tried to capture threadId during the constructor, but somewhere along the lifetime of the program threadId moves.
In my project, some secondary thread processes need access to a newly created object.
I use a producer-consumer pattern so that they can be created in a Unity stream. The factory object queues for the request, and on Update() objects I requested are created in the correct chain. Between Queued and Instantiated, the factory method expects an ObjectCreated event using AutoResetEvent.
Now sometimes this factory is called from the main thread, and AutoResetEvent blocks its own thread. I also tried this in a dirty way with
// First try on this thread try { return action(); } catch (ArgumentException ex) { Debug.Log("Tried on same thread, but failed. "+ex.Message); } PushToQueueAndWait(action);
But when unity throws an exception, caught or not, the program stops.
If I could check if I was in the correct thread, I could switch between the queue and just execute.
multithreading c # unity3d producer-consumer
Boris Callens
source share