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

Are co-routine callbacks thread-safe?

Context:



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.

Example:



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

callback(request);
}

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


Can someone who has experienced this confirm that coroutines are indeed synchronous or
GlobalVar
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