forked from HSerg/luathread-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.html
229 lines (186 loc) · 6.43 KB
/
reference.html
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="description" content="LuaThread: Reference Manual">
<meta name="keywords" content="Lua, Library, Manual, Multi-Threading, Threads, Support">
<title>LuaThread: Multi-(platform|threading) support for the Lua language
</title>
<link rel="stylesheet" href="reference.css" type="text/css">
</head>
<body>
<!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<div class=header>
<hr>
<center>
<table summary="LuaThread logo">
<tr><td align=center><a href="http://www.lua.org">
<img width=128 height=128 border=0 alt="LuaThread" src="luathread.png">
</a></td></tr>
<tr><td align=center valign=top>Multi-(platform|threading) support for the
Lua language
</td></tr>
</table>
<p class=bar>
<a href="home.html">home</a> ·
<a href="installation.html">installation</a> ·
<a href="examples.html">examples</a> ·
<a href="reference.html">reference</a>
</p>
</center>
<hr>
</div>
<!-- Reference ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<h2>Reference</h2>
<!-- newthread ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=newthread>
thread.<b>newthread(</b>func<b>, </b>arg<b>)</b>
</p>
<p class=description>
Creates a new thread of execution.
</p>
<p class=arguments>
<tt>Func</tt> is the function that will be executed in the new thread.
<tt>Arg</tt> is a table with the arguments to be passed to <tt>func</tt>
as in <tt>func(unpack(arg))</tt>.
</p>
<p class=description>
The new execution thread lasts until <tt>func</tt> returns.
</p>
<!-- newmutex +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=newmutex>
thread.<b>newmutex()</b>
</p>
<p class=description>
Creates new <em>mutex</em>, a mutual exclusion
synchronization object. A mutex can be in locked or unlocked
state (initially unlocked). The thread has locked a mutex is
said to <em>own</em> the mutex. A mutex can be owned by only
one thread at any given time. If a thread attempts to lock a mutex owned by
another thread, it will be blocked until the owner thread unlocks the
mutex.
</p>
<p class=return>
The function returns the newly created unlocked mutex.
</p>
<p class=note>
Note: LuaThread mutexes are recursive in the sense that successive calls
to <tt>mutex:lock()</tt> nest. If the mutex owner locks a mutex
<tt>n</tt> times, it has to unlock the mutex the same <tt>n</tt> times
before the mutex switches to unlocked state.
</p>
<!-- mutex:lock +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id="mutex:lock">
mutex:<b>lock()</b>
</p>
<p class=description>
Locks a mutex. If the mutex is owned by
another thread, the function blocks the caller thread until
the owner unlocks the mutex.
</p>
<!-- mutex:unlock +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id="mutex:unlock">
mutex:<b>unlock()</b>
</p>
<p class=description>
Unlocks the given mutex. Calling <tt>mutex:unlock()</tt> without
owning the mutex is an error. </p>
<!-- newcond ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=newcond>
thread.<b>newcond()</b>
</p>
<p class=description>
Creates and a new <tt>cond</tt>, a condition variable. Condition
variables allow threads to wait for a predicate to be satisfied, and to
signal to waiting threads that the predicate status has been changed.
<p class=description>
The condition variable is used in association with a mutex that
protects the predicate from race conditions. Before waiting or signaling
a condition variable, the mutex associated with the predicate has to be
locked.
</p>
<p class=return>
The function returns the newly created condition variable.
</p>
<!-- cond:wait ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id="cond:wait">
cond:<b>wait(</b>mutex<b>)</b>
</p>
<p class=description>
Waits on a condition variable.
</p>
<p class=parameters>
<tt>Mutex</tt> is the mutex associated with the predicate. In order to
use this function, the calling thread has to own the mutex.
Before the thread is put to wait (if need be), however,
the mutex is unlocked (see <a href=#safe>comments</a>).
Upon return, the thread owns the mutex again.
</p>
<p class=note> To wait on a condition variable,
proceed as follows:
</p>
<pre class=example>
-- make sure we are the only currently accessing the predicate
predicate.mutex:lock()
-- while the predicate is not what we want
while unsatisfactory(predicate) do
-- wait until someone changes the predicate
predicate.cond:wait(predicate.mutex)
end
-- at this point, the predicate is valid AND we own it
use(predicate)
-- unlock the mutex, so that other threads can access the predicate
predicate.mutex:unlock()
</pre>
<p class=note id=safe>Note: LuaThread conditions are safe in the
sense that even if a thread has more than one lock over a
<tt>mutex</tt>,
<tt>cond:wait(mutex)</tt> manages to unlock the <tt>mutex</tt> before
blocking the caller thread. Upon return, the same number of locks is
restored into the <tt>mutex</tt>.
</p>
<!-- cond:signal ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id="cond:signal">cond:<b>signal()</b>
</p>
<p>
Releases one of the threads blocked on the condition variable.
Before signaling, the caller thread should own the mutex
associated to the predicate.
</p>
<p>To signal a condition variable, proceed as follows: </p>
<pre class=example>
-- make sure we are the only currently accessing the predicate
predicate.mutex:lock()
-- change the predicate
update(predicate)
-- release one of the threads blocked on cond
predicate.cond:signal()
-- unlock the mutex, so that other threads can access the predicate
predicate.mutex:unlock()
</pre>
<p class=name id="cond:broadcast">cond:<b>broadcast()</b>
</p>
<p class=description>
Similar to <tt>cond:signal()</tt>, but releases all threads blocked
on the condition variable.
</p>
<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<div class=footer>
<hr>
<center>
<p class=bar>
<a href="home.html">home</a> ·
<a href="installation.html">installation</a> ·
<a href="examples.html">examples</a> ·
<a href="reference.html">reference</a>
</p>
<p>
<small>
Last modified by Diego Nehab on <br>
Thu Jun 17 02:47:14 EDT 2004
</small>
</p>
</center>
</div>
</body>
</html>