Computer Science Canada Process question |
Author: | battfel [ Fri Oct 22, 2021 5:54 pm ] | ||
Post subject: | Process question | ||
My goal here is for damage to come out as 3. Why doesn't something like this work? it comes out as 0. I'm very new to programming and turing
Edit : I realised if I put a delay between stick := true, and put damage, it works. Even just delay(1). This isn't very practical in my bigger project though, why is it that there has to be this delay? |
Author: | Insectoid [ Sun Oct 24, 2021 6:39 pm ] |
Post subject: | RE:Process question |
Processes don't run in order, and they don't run simultaneously. They take turns running code, pausing to let other processes go and then resuming from where they left off. In this case, when you fork thing, it doesn't run right away. 'put damage' actually runs first. If you run the program over and over, you'll notice that sometimes damage = 3, and sometimes damage=0. As you've discovered, processes aren't much use in Turing, except for playing music. Fortunately, we have functions and procedures in Turing, which look just like processes in the code, but run sequentially. To write a procedure, just write 'procedure' (or just 'proc') instead of 'process', and don't write the word 'fork'. If you modify your program to use a procedure, you should find that it works the way you expect. |