##- # Author: Brian Tiffin # Dedicated to the public domain # # Date: September 2016 # Modified: 2016-10-13/22:57-0400 ##+ # # thread.icn, Demonstrate thread messaging # # requires Concurrency build of Unicon # procedure main() pr := create producerRace() cr := create consumerRace(pr) pTr := spawn(pr) cTr := spawn(cr) every wait(pTr | cTr) write("racing producer/consumer complete") write() p := create producer() c := create consumer(p) pT := spawn(p) cT := spawn(c) every wait(pT | cT) write("main complete") end # # This code can easily trigger incorrect results due to a race condition # # send messages to the thread out-box procedure producerRace() write("producer entry") every !6@> end # receive messages from the out-box of the producer thread procedure consumerRace(T) write("consumer entry") while write(<@T) end # What follows is the suggested update from Jafar # It alleviates the race condition where consumerRace # can complete before the producerRace even starts # # an original capture: # # JMBPro:proj jafar$ ./thrd # producer entry # consumer entry # racing producer/consumer complete # # This is the better code... # # send messages to the thread out-box procedure producer() write("synch producer entry") every !6@> # produce &null (&null@>) to signal the end @> end # receive messages from the out-box of the producer thread procedure consumer(T) write("blocking consumer entry") # blocking receive. while write(\<<@T) end