aboutsummaryrefslogtreecommitdiff
path: root/docs/thread.md
blob: 32df9c9f89ba7462b24dc31599ed2d7e3b670315 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# threads **

## lock, unlock**

'takes an integer

locks any other thread reaching this lock id until a corresponding unlock is met

```lua
llib.thread.lock(5)
...
llib.thread.unlock(5)
```

more indepth

```lua
local t = llib.thread.async(function(info)
    ...
    llib.thread.lock(5)
    ...
    return N
end)

...
llib.thread.unlock(5)
t:await()
```

## aync **

'takes a function which will be ran in a separate thread with a single parameter with thread info

these have the same backend (and limitations) of network threads

```lua
local thread = llib.thread.async(function(res, rej)
    local N = 0
    ...
    res(N)
end)
```

### thread function parameters **

as used with "res" above

#### res:res() **

'takes any amount of "any" values

send a value(s) to thread:await() call then stalls the thread until cleaned
#### res:send() **

'takes "any" value

send a value which can be retrieved from outside the thread with thread:next()

```lua
info:send(5)
info:send("hello")
```

### thread return object **

#### thread:await() **

'optional timeout in ms and boolean whether to keep or not

waits for the thread to return, and returns whatever it returned then closes it, or nil if timeout was exceeded
if the input is the boolean value true, it will keep the thread alive (otherwise await() can not be called again)

```lua
thread:await() -- value of N (above)
```

```lua
thread:await(20) -- value of N (above) or nil
```

```lua
thread:await(true) -- value of N (above)
thread:await() -- same
thread:await() -- error, function above performed cleanup
```

#### thread:next() **

gets the most oldest value sent using info:send() and pops it

```lua
--(continued from above)
thread:next() -- 5
thread:next() -- "hello"
```

#### thread:kill() **

kills the thread