summaryrefslogtreecommitdiff
path: root/tests/modules/thread/mutex.lua
blob: 472b228db88f33c9c7701c66803dff8670ca6b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import("core.thread.thread")
import("core.thread.mutex")

function callback(mutex)
    import("core.thread.thread")
    print("%s: starting ..", thread.running())
    local dt = os.mclock()
    for i = 1, 10 do
        mutex:lock()
        print("%s: %d", thread.running(), i)
        os.sleep(1000)
        mutex:unlock()
    end
    dt = os.mclock() - dt
    print("%s: end, dt: %d ms", thread.running(), dt)
end

function main()
    local m = mutex.new()
    local t0 = thread.start_named("thread_0", callback, m)
    local t1 = thread.start_named("thread_1", callback, m)
    t0:wait(-1)
    t1:wait(-1)
end