If I use multi threading - for example, using @spawn top()
- and the body of top()
also invokes either @async
or even @spawn
of some nested()
work, and uses @sync()
to wait for this work;
Then I assume that the Julia scheduler is free to run other tasks on the thread (e.g., while top()
is waiting on a @sync
). That is, top()
may be paused and waiting for the scheduler to resume it.
Is it guaranteed that top()
will only be resumed on the thread it started on? That is, if I write thread_id = Threads.threadid()
in the first line of top()
, can I safely @assert thread_id == Threads.threadid()
in the last line of top()
?
Or can the scheduler resume top()
in another thread, so that the value of Threads.threadid()
might change following a @sync
(and possibly in other points), so such an assertion is not safe to make?
Edit: It seems that today the answer is this assertion is safe. The question then becomes whether this is guaranteed to hold in future versions of Julia, or whether it is possible for a future version scheduler to break this promise.