-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample06.lua
68 lines (56 loc) · 3 KB
/
example06.lua
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
--[[
This examples demonstrates the usage of the *Notify C API* in a multithreading scenario.
Mtstates' state objects implement the *Notify C API*, see [src/notify_capi.h](../src/notify_capi.h),
i.e. the state object has an an associated meta table entry *_capi_notify* delivered by
the C API function *notify_get_capi()* and the associated C API function *toNotifier()* returns
a valid pointer for a given state object.
In this example the *Notify C API* is used to notify a state object by invoking the state's
callback function without arguments each time a message is added to a [mtmsg](https://github.com/osch/lua-mtmsg)
buffer object.
This is done by connecting the state object as a notifier object to the mtmsg buffer
object.
--]]
local llthreads = require("llthreads2.ex")
local mtmsg = require("mtmsg")
local mtstates = require("mtstates")
local buffer = mtmsg.newbuffer()
local state = mtstates.newstate(function(bufferId)
local mtmsg = require("mtmsg")
local buffer = mtmsg.buffer(bufferId)
local list = {}
local cmds = {
add = function(arg) list[#list + 1] = arg end,
get = function() return table.concat(list, " ") end
}
return function(cmd, ...)
if cmd then
return cmds[cmd](...)
else
assert(select("#", ...) == 0)
local a1, a2 = buffer:nextmsg()
print("State received", a1, a2)
list[#list + 1] = a1
list[#list + 1] = a2
end
end
end, buffer:id())
buffer:notifier(state) -- state will be notified each time a message is added to the buffer
local thread = llthreads.new(function(bufferId)
local mtmsg = require("mtmsg")
local buffer = mtmsg.buffer(bufferId)
for i = 2, 4 do
print("Thread puts into buffer ", i)
buffer:addmsg("fromThread:", i)
end
end,
buffer:id())
state:call("add", "init")
buffer:addmsg("fromMain:", 1)
print("Starting Thread")
thread:start()
thread:join()
print("Thread finished")
buffer:addmsg("fromMain:", 5)
local list = state:call("get")
print("list:", list)
assert(list == "init fromMain: 1 fromThread: 2 fromThread: 3 fromThread: 4 fromMain: 5")