Updated example from Ross Anderson.

Includes a change to allow passing in the number of sub-threads to
create from the command line.
master
Robert G. Jakabosky 15 years ago
parent 88a4fcd946
commit c95f133003

@ -18,36 +18,48 @@
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
-- Sub-thread processing example in Lua using llthreads - 1,000 quick sub-thread execution
-- luajit sub_threads.lua
local llthreads = require"llthreads"
--- level 0 string literal enclosure [[ ]]
local num_threads = tonumber(arg[1] or 1000)
-- level 0 string literal enclosure [[ ]] of child execution code
local thread_code = [[
print("CHILD: received params:", ...)
local llthreads = require"llthreads" -- need to re-declare under this scope
local num_threads = ...
print("CHILD: received from ROOT params:", ...)
local llthreads = require"llthreads" -- need to re-declare this under this scope
local t = {} -- thread storage table
-- create a new child thread - with level 1 literal string [=[ ]=] enclosures, level 2 would be [==[ ]==]
-- create a new child sub-thread execution code - it requires level 1 literal string [=[ ]=] enclosures, level 2 would be [==[ ]==]
local executed_child_code = [=[
return 'Hello from child sub-thread, new input params:', ...
return "Hello from child sub-thread, new input params:", ...
]=]
-- first child sub-thread
local thread1 = llthreads.new(executed_child_code , "number:", 1001, "nil:", nil, "bool:", true)
assert(thread1:start()) -- start new child sub-thread
table.insert(t, 1, thread1) -- insert thread into thread table
-- second child sub-thread
local thread2 = llthreads.new(executed_child_code , "number:", 1002, "nil:", nil, "bool:", true)
assert(thread2:start()) -- start new child sub-thread
table.insert(t, 1, thread2) -- insert thread into thread table
-- wait for all child sub-threads to complete before returning to root
-- create 1000 sub-threads - which creates an incremental 30% / 20% utilization spike on the two AMD cpu cores
print("CHILD: Create sub threads:", num_threads)
for i=1,num_threads do
-- create child sub-thread with code to execute and the input parmeters
local thread = llthreads.new(executed_child_code , "number:", 1000 + i, "nil:", nil, "bool:", true)
assert(thread:start()) -- start new child sub-thread
table.insert(t, thread) -- append the thread at the end of the thread table
end
-- wait (block) for all child sub-threads to complete before returning to ROOT
while true do
print("PARENT: child returned: ", t[1]:join())
table.remove(t,1)
if (#t <= 0) then break end
-- always wait on the first element, since order is not important
print("CHILD: sub-thread returned: ", t[1]:join())
table.remove(t,1) -- always remove the first element
if (#t == 0) then break end
end
return ... -- return the parents' input params back to the root
]]
-- create child thread.
local thread = llthreads.new(thread_code, "number:", 1234, "nil:", nil, "bool:", true)
local thread = llthreads.new(thread_code, num_threads, "number:", 1000, "nil:", nil, "bool:", true)
-- start joinable child thread.
assert(thread:start())
print("Root: child returned: ", thread:join())
-- wait for all child and child sub-threads to finish
print("ROOT: child returned: ", thread:join())

Loading…
Cancel
Save