53 lines
1.2 KiB
Text
53 lines
1.2 KiB
Text
-- demo\rosetta\Synchronous_concurrency.exw
|
|
without js -- threads, file i/o, command_line()
|
|
string filename = substitute(command_line()[2],".exe",".exw")
|
|
|
|
atom frThread, -- file reader thread
|
|
lcThread -- line counter thread
|
|
|
|
sequence queue = {}
|
|
integer qlock = init_cs(),
|
|
linecount = 1
|
|
|
|
procedure readfile()
|
|
integer fn = open(filename,"r")
|
|
while 1 do
|
|
object line = gets(fn)
|
|
enter_cs(qlock)
|
|
queue = append(queue,line)
|
|
line = atom(line) -- kill refcount!
|
|
leave_cs(qlock)
|
|
if line then exit end if
|
|
end while
|
|
close(fn)
|
|
wait_thread(lcThread)
|
|
printf(1,"Lines read: %d\n",linecount)
|
|
exit_thread(0)
|
|
end procedure
|
|
|
|
procedure countlines()
|
|
linecount = 0
|
|
while 1 do
|
|
enter_cs(qlock)
|
|
if length(queue)=0 then
|
|
leave_cs(qlock)
|
|
-- sleep(0.1)
|
|
else
|
|
object line = queue[1]
|
|
queue = queue[2..$]
|
|
leave_cs(qlock)
|
|
if atom(line) then exit end if
|
|
-- ?line
|
|
linecount += 1
|
|
end if
|
|
end while
|
|
exit_thread(0)
|
|
end procedure
|
|
|
|
lcThread = create_thread(countlines,{})
|
|
frThread = create_thread(readfile,{})
|
|
|
|
wait_thread(frThread)
|
|
puts(1,"done")
|
|
|
|
{} = wait_key()
|