Pooh program example 15-thread-range.p
# co-routine threads: create thread via API, thread yields a range of numbers. sub myrange( from, to ) i = from while i <= to # yield function puts this thread to sleep and will wake up the caling thread # it also returns a value to the calling thread. threadyield0( ~yieldval i ) i = i + 1 end end # # Invoke the co-routine via the API. # th := makethread( ~func myrange ) th( ~from 1 ~to 10) [ running, value ] = threadyieldvalue( ~thread th ) while( running != 0 ) println( ~msg 'thread returned [ value ]' ) [ running, value ] = resumethread( ~thread th ) end stopthread( ~thread th )
thread returned 1 thread returned 2 thread returned 3 thread returned 4 thread returned 5 thread returned 6 thread returned 7 thread returned 8 thread returned 9 thread returned 10Trace output for 15-thread-range.p
020|... := makethread( ~func sub myrange(~from , ~to) )... 020|th := makethread( ~func sub myrange(~from , ~to) ):-> sub myrange 022|th:-> sub myrange( ~from 1 ~to 10 )... 004| i = from:-> 1 005| while (i:1 <= to:-> 10):true 010| threadyield0( ~yieldval i:1 )... 024| ... = threadyieldvalue( ~thread th:-> sub myrange(~from , ~to) )... 024| [ running:1 , value:-> 1] = threadyieldvalue( ~thread th:-> sub myrange(~from , ~to) ):-> [ 1, -> 1 ] 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:1 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:1 + 1):2 013| end 005| while (i:2 <= to:-> 10):true 010| threadyield0( ~yieldval i:2 )... 030| [ running:1 , value:-> 2] =threadyield0( ~yieldval i:2 ):-> [ 1, -> 2 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:2 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:2 + 1):3 013| end 005| while (i:3 <= to:-> 10):true 010| threadyield0( ~yieldval i:3 )... 030| [ running:1 , value:-> 3] =threadyield0( ~yieldval i:3 ):-> [ 1, -> 3 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:3 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:3 + 1):4 013| end 005| while (i:4 <= to:-> 10):true 010| threadyield0( ~yieldval i:4 )... 030| [ running:1 , value:-> 4] =threadyield0( ~yieldval i:4 ):-> [ 1, -> 4 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:4 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:4 + 1):5 013| end 005| while (i:5 <= to:-> 10):true 010| threadyield0( ~yieldval i:5 )... 030| [ running:1 , value:-> 5] =threadyield0( ~yieldval i:5 ):-> [ 1, -> 5 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:5 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:5 + 1):6 013| end 005| while (i:6 <= to:-> 10):true 010| threadyield0( ~yieldval i:6 )... 030| [ running:1 , value:-> 6] =threadyield0( ~yieldval i:6 ):-> [ 1, -> 6 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:6 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:6 + 1):7 013| end 005| while (i:7 <= to:-> 10):true 010| threadyield0( ~yieldval i:7 )... 030| [ running:1 , value:-> 7] =threadyield0( ~yieldval i:7 ):-> [ 1, -> 7 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:7 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:7 + 1):8 013| end 005| while (i:8 <= to:-> 10):true 010| threadyield0( ~yieldval i:8 )... 030| [ running:1 , value:-> 8] =threadyield0( ~yieldval i:8 ):-> [ 1, -> 8 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:8 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:8 + 1):9 013| end 005| while (i:9 <= to:-> 10):true 010| threadyield0( ~yieldval i:9 )... 030| [ running:1 , value:-> 9] =threadyield0( ~yieldval i:9 ):-> [ 1, -> 9 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:9 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:9 + 1):10 013| end 005| while (i:10 <= to:-> 10):true 010| threadyield0( ~yieldval i:10 )... 030| [ running:1 , value:-> 10] =threadyield0( ~yieldval i:10 ):-> [ 1, -> 10 ] 031| end 026| while (running:1 != 0):true 028| println( ~msg 'thread returned ' .. value:10 .. '' )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) )... 030| ... = resumethread( ~thread th:-> sub myrange(~from , ~to) ):-> Null 012| i = (i:10 + 1):11 013| end 005| while (i:11 <= to:-> 10):false 013| end # finish loop 030| [ running:0 , value:Null] = 031| end 026| while (running:0 != 0):false 031| end # finish loop 033| stopthread( ~thread th:-> sub myrange(~from , ~to) )...