The task switcher happens inside a call to println("A") , which at some point calls write(STDOUT, "A".data) . Since isa(STDOUT, Base.AsyncStream) and there is no more specialized method, this allows:
write{T}(s::AsyncStream,a::Array{T}) at stream.jl:782
If you look at this method, you will notice that it calls stream_wait(ct) for the current ct task, which in turn calls wait() .
(Also note that println not atomic, as there is a wait potential between writing arguments and a new line.)
Of course, you can determine when this happens if you look at all the code used. But I donβt understand why you need to know this for sure, because when working with parallelism you should not depend on processes that do not change the context. If you are dependent on a specific execution order, synchronize explicitly.
(You already noticed this in your question somehow, but let me repeat it here: as a rule, when using green threads, you can expect potential context switches when doing I / O, since locking for I / O is an example of a tutorial why green threads useful first.)
source share