Your code is not thread-safe on many levels. Even if you replace regular increments/decrements with methods from
Interlockedclass, you will still need additional synchronization to make sure, thatCallbackis not called whileStartis running.Term
coroutinehas a specific meaning in context of Unity, so you should probably use different word to avoid confusion.I don't know what is the recommended way of doing these things in Unity. In absence of TPL (I assume Unity still uses .Net 3.5), the easiest solution I can think of, that does not require good understanding of threading, is to return WaitHandles from your async methods:
void Start() { var handles = new List<WiatHandle>(); handles.Add(MyLogger.LogLastMonth()); handles.Add(MyParser.ParseFiles()); //etc... ThreadPool.QueueUserWorkItem(_ => { WaitHandle.WaitAll(handles.ToArray()); //at this point you might want to dispose handles //if you do not plan to re-use them //Make sure this call is thread-safe. //If it is not, you will have to dispatch this call back to main thread //this callor backset tosome mainbool threadfield here, and move the call to Update. SceneManager.LoadScene("GameScene"); }); }