From 8a331ab64fe560924b8bc3ab3dc4ac2831dd8920 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 28 Aug 2025 22:45:48 +0800 Subject: add thread semaphore --- tests/modules/thread/semaphore.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/modules/thread/semaphore.lua (limited to 'tests/modules/thread/semaphore.lua') diff --git a/tests/modules/thread/semaphore.lua b/tests/modules/thread/semaphore.lua new file mode 100644 index 000000000..b05774e1d --- /dev/null +++ b/tests/modules/thread/semaphore.lua @@ -0,0 +1,25 @@ +import("core.base.thread") + +function callback(semaphore) + import("core.base.thread") + print("%s: starting ..", thread.running()) + while true do + print("%s: waiting ..", thread.running()) + if semaphore:wait(-1) > 0 then + print("%s: triggered", thread.running()) + end + end +end + +function main() + local semaphore = thread.semaphore("", 1) + local t = thread.start_named("keyboard", callback, semaphore) + while true do + local ch = io.read() + if ch then + semaphore:post(2) + end + end + t:wait(-1) +end + -- cgit v1.3.1