https://rosettacode.org/wiki/Synchronous_concurrency#M2000_Interpreter
Synchronous concurrency is a task in rosettacode.
module TwoThreads {
rem {
For this example we split the screen to two layers
the left layer is the bacic layer, no need to exclusive address it
.. we can using Layer { }
the right layer is Layer 1 we use this for print_unit
Also, when we use threads we handle screen refresh using REFRESH
}
' M2000 Window: using current font size and current monitor full screen
// font "Arial"
// font "Courier"
font "Courier new"
window mode, window
escape off ' code 27 now work on the main.task (using KEYPRESS(27))
// thread.plan sequential ' execute a thread and the switch to other thread
thread.plan concurrent ' execute one statement and switch thread
enum action {printline, returnline, closeall}
var finish as boolean, p as long, current=printline
const curscale.x=scale.x, curscale.y=scale.y, mx=motion.x, my=motion.y
mode mode, scale.x/2,scale.y
motion mx ' set left property of layer to mx
' prepare the input.txt (wide for UTF16LE)
open "input.txt" for wide output as #f
for i=1 to 20
print #f, string$("*", random(1, 30))
next
close #f
layer 1{
window mode, curscale.x/2, curscale.y
motion mx+curscale.x/2, my
pen 11
cls 1
show
print $(,8), ' set column width
thread {
Static lines2print=0&
if not empty then
if number=printline then
lines2print++
print lines2print, ". ";letter$ : refresh
end if
end if
} as print_unit interval 50
}
print "done": refresh
thread {
if p else
open "input.txt" for wide input as #p
end if
if current=returnline then
thread print_unit execute finish=empty ' check stack of print_unit
if finish then
thread print_unit execute {
Print "lines: ";lines2print
refresh
}
thread print_unit erase
threads ' 2
current++
end if
else.if eof(#p) then
current++
close#p
else
line input #p, buffer$
thread print_unit execute data printline, buffer$
end if
} as reading_unit interval 50
' main.task is a thread also
main.task 50 {
if keypress(27) or current=closeall then exit
// threads ' number of threads
}
threads erase
threads ' number of threads
escape on
}
TwoThreads
