Ja. L Ja. L - 1 year ago 47
C# Question

Are co-routine callbacks thread-safe?


Unity Co-routines are not multi-threaded according to this Article. Therefore, if coroutine callbacks are accessing a global variable, I would assume there will not be thread conflicts.


private IEnumerator Resolve(WWW request, Action<WWW> callback)
while (!request.isDone)
yield return null;


//Invocation - called constantly
StartCoroutine(Resolve(request, request => {GlobalVar = request.text;}));

Can someone who has experienced this confirm that coroutines are indeed synchronous or
is Thread-Safe?

Answer Source

I had an argument in the comments with someone about this recently.

I can't find those comments now, but... Found it.

No, coroutines are not threads and cannot encounter a race condition

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download