For example if a sprite gets two messages simultaneously but must service them one at a time. It seems that an application implementing massive concurrency should have some sort of arbiter, semaphore, atomic test and set and so on. I cannot find any way to accomplish it in the manual.
Hm...back then there was a way to get certain custom messages through a variable block, without the use of the WHEN I RECIEVE [MESSAGE] block. It doesn't appear in the normal version of Snap!, but it does make an appearance when you enable the Development Version of Snap!.
Shift + Click the Snap! logo and then click on "Switch to Development Editor".
Okay, by now you're wondering: what does this have to do with anything? Well, to that I say: you can make custom messages and stuff, you can embed a code (like 1PrintText could make Sprite 1 print a certain text without the interference of other Sprites) and then you can use the Message Variable Block to access and receive these messages.
Yeah...my explanations have room for improvement, but...let me know if you don't understand and I can try to explain it in a better way.
You have to understand that the Snap! scheduler doesn't switch between threads at arbitrary points in the code. Threads yield explicitly when certain situations happen, most notably at the bottom of each iteration of a loop block. What's most important to you is that there is never a yield between the test of an IF-like block and the first instruction in its enclosed script. This guarantee is equivalent to a test-and-set. Similarly, there is never a yield between a WHEN hat block at the first instruction in the script headed by the hat block. (Exception: if the test you put in a generic WHEN block takes a long time, it might be interrupted.)
If you happen to be teaching about concurrency and explicit mechanisms for critical sections, you can write your own scheduler using continuations, or you can put explicit yields (WAIT 0 SECONDS) in your code.
This is very helpful. If I understand you correctly the test script within a generic when block will not yield unless it is interrupted. How and when do interrupts occur. It seems that an external interrupt should not affect the scheduler. Are there internal interrupts that affect the scheduling of threads?
Generic WHEN is a special case. All the other hat blocks involve guaranteed fast tests, but in generic WHEN the user provides the test. So we set a timer for, I forget, some long amount of time, and report an error if it goes off.
Thank you. Very helpful.
This post was flagged by the community and is temporarily hidden.