This question is out of my wheelhouse, but interesting… so, I reached out to a few of the software engineers that write the code that runs your code for their input…Here is a summery of their feedback.
He can go ahead and pass the flag lock variable; when you pass in a variable, the subroutine operates on the actual variable, not a copy.
Charts enter a loop while they’re waiting for the flag lock variable, but there’s a 1 msec delay at the top of the loop; the only thing the chart does while in that state is to check for messages, (like STOP or QUIT), and delay. CPU usage during that time will be negligible.
Perhaps they are thinking that a subroutine is like a Call Chart; it’s not. Charts that call a subroutine don’t pause; their execution branches to the subroutine, which means the chart’s other instructions can’t be executed until the subroutine completes and returns execution back to the chart.
The very high level sequence of events for locking the flag looks like this:
Do
Attempt lock
If failed
Check for messages (Like stop or quit)
Delay 1 msec
Endif
While failed and not (stop or quit)
I hope that between @philip typically excellent information / experience and this insight you can go forth and code in confidence.