Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Process question
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
battfel




PostPosted: 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


code:
var damage : int := 0
var stick : boolean := false

process thing
    if stick = true then
        damage := damage + 3
    end if
end thing

fork thing
stick := true
put damage




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?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: