summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/luv/examples/uvbook/thread-create.lua
blob: 4b42587adbf612eb3b01b5e6ea3eae684deeac52 (plain) (blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
local uv = require('luv')

local step = 10

local hare_id = uv.new_thread(function(step,...)
    local ffi = require'ffi'
    local uv = require('luv')
    local sleep 
    if ffi.os=='Windows' then
        ffi.cdef "void Sleep(int ms);"
        sleep = ffi.C.Sleep
    else 
        ffi.cdef "unsigned int usleep(unsigned int seconds);"
        sleep = ffi.C.usleep
    end
    while (step>0) do 
        step = step - 1
        uv.sleep(math.random(1000))
        print("Hare ran another step")
    end
    print("Hare done running!")
end, step,true,'abcd','false')

local tortoise_id = uv.new_thread(function(step,...)
    local uv = require('luv')
    while (step>0) do 
        step = step - 1
        uv.sleep(math.random(100))
        print("Tortoise ran another step")
    end
    print("Tortoise done running!")
end,step,'abcd','false')

print(hare_id==hare_id,uv.thread_equal(hare_id,hare_id))
print(tortoise_id==hare_id,uv.thread_equal(tortoise_id,hare_id))

uv.thread_join(hare_id)
uv.thread_join(tortoise_id)