From 628371d0edea22eb177fa2b1a6b9f28e8c0cff2d Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Aug 2025 23:14:13 +0800 Subject: add thread queue --- tests/modules/thread/queue.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/modules/thread/queue.lua (limited to 'tests/modules/thread/queue.lua') diff --git a/tests/modules/thread/queue.lua b/tests/modules/thread/queue.lua new file mode 100644 index 000000000..36034993f --- /dev/null +++ b/tests/modules/thread/queue.lua @@ -0,0 +1,30 @@ +import("core.base.thread") + +function callback(event, queue) + import("core.base.thread") + print("%s: starting ..", thread.running()) + while true do + print("%s: waiting ..", thread.running()) + if event:wait(-1) > 0 then + print("%s: triggered", thread.running()) + end + while not queue:empty() do + print("%s: get %s", thread.running(), queue:pop()) + end + end +end + +function main() + local event = thread.event() + local queue = thread.queue() + local t = thread.start_named("keyboard", callback, event, queue) + while true do + local ch = io.read() + if ch then + queue:push(ch) + event:post() + end + end + t:wait(-1) +end + -- cgit v1.3.1 From 3a1890f777eec156fd4e85616e9487a4fdc92abc Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Aug 2025 23:16:25 +0800 Subject: improve tests --- tests/modules/thread/queue.lua | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'tests/modules/thread/queue.lua') diff --git a/tests/modules/thread/queue.lua b/tests/modules/thread/queue.lua index 36034993f..ebf298ec2 100644 --- a/tests/modules/thread/queue.lua +++ b/tests/modules/thread/queue.lua @@ -1,15 +1,13 @@ import("core.base.thread") function callback(event, queue) - import("core.base.thread") - print("%s: starting ..", thread.running()) + print("starting ..") while true do - print("%s: waiting ..", thread.running()) + print("waiting ..") if event:wait(-1) > 0 then - print("%s: triggered", thread.running()) - end - while not queue:empty() do - print("%s: get %s", thread.running(), queue:pop()) + while not queue:empty() do + print(" -> %s", queue:pop()) + end end end end @@ -17,7 +15,7 @@ end function main() local event = thread.event() local queue = thread.queue() - local t = thread.start_named("keyboard", callback, event, queue) + local t = thread.start_named("", callback, event, queue) while true do local ch = io.read() if ch then -- cgit v1.3.1