Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

libthread

A thread library that's simple to use.

Developers

  • Zer0Galaxy

Description

Important: OpenOS 1.6.4 and above includes its own threading library. Please use it instead.

API

  • 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.

Example

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()

Screenshot 1: the result of executing the code above

Links