A thread library that's simple to use.
- Zer0Galaxy
Important: OpenOS 1.6.4 and above includes its own threading library. Please use it instead.
thread.init()— MUST BE CALLED first before calling other functions of the library.thread.create(f: function, ...)— convert a function to a thread that starts immediately. You can provide the arguments that will be passed to the function.thread.kill(thread: thread)— kills a thread.thread.killAll()— kills all threads but the main one.thread.waitForAll()— waits until all child threads end. All child threads are killed if the main one dies, so you should call this function at the end of your progarm to give the child threads a chance to end correctly.
local thread = require("thread")
-- Initialize the library
thread.init()
-- Function that prints the string several times with a 1-second interval.
local function foo(str,n)
for i = 1, n do
print(str)
os.sleep(1)
end
end
-- Create two threads with different parameters.
thread.create(foo, "AAA", 5)
thread.create(foo, "BBB", 7)
-- Wait until the child threads die.
thread.waitForAll()