diff options
author | Alexey Melnichuk <mimir@newmail.ru> | 2014-02-11 17:32:59 +0400 |
---|---|---|
committer | Alexey Melnichuk <mimir@newmail.ru> | 2014-02-11 17:34:14 +0400 |
commit | d13929706a3b45bd64e0a87e0afc3d45625e888d (patch) | |
tree | d1c846805f127736d1dafb3b3094eaf78d032859 /llthreads2/test/test_threads.lua | |
parent | 46ed59584e5407c49a02f1ea6bede6487259a92e (diff) | |
download | lua-llthreads2-d13929706a3b45bd64e0a87e0afc3d45625e888d.tar.gz lua-llthreads2-d13929706a3b45bd64e0a87e0afc3d45625e888d.tar.bz2 lua-llthreads2-d13929706a3b45bd64e0a87e0afc3d45625e888d.zip |
Init LuaDist for llthreads2 module.
Diffstat (limited to 'llthreads2/test/test_threads.lua')
-rw-r--r-- | llthreads2/test/test_threads.lua | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/llthreads2/test/test_threads.lua b/llthreads2/test/test_threads.lua new file mode 100644 index 0000000..459604d --- /dev/null +++ b/llthreads2/test/test_threads.lua | |||
@@ -0,0 +1,67 @@ | |||
1 | -- Copyright (c) 2011 by Ross Anderson <ross_j_anderson@yahoo.com> | ||
2 | -- | ||
3 | -- Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4 | -- of this software and associated documentation files (the "Software"), to deal | ||
5 | -- in the Software without restriction, including without limitation the rights | ||
6 | -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
7 | -- copies of the Software, and to permit persons to whom the Software is | ||
8 | -- furnished to do so, subject to the following conditions: | ||
9 | -- | ||
10 | -- The above copyright notice and this permission notice shall be included in | ||
11 | -- all copies or substantial portions of the Software. | ||
12 | -- | ||
13 | -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
14 | -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
15 | -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
16 | -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
17 | -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
18 | -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
19 | -- THE SOFTWARE. | ||
20 | |||
21 | -- Sub-thread processing example in Lua using llthreads - 1,000 quick sub-thread execution | ||
22 | |||
23 | -- luajit sub_threads.lua | ||
24 | |||
25 | local llthreads = require"llthreads" | ||
26 | local utils = require "utils" | ||
27 | |||
28 | local num_threads = tonumber(arg[1] or 1000) | ||
29 | |||
30 | -- level 0 string literal enclosure [[ ]] of child execution code | ||
31 | local thread_code = utils.thread_init .. [[ | ||
32 | |||
33 | local num_threads = ... | ||
34 | print("CHILD: received from ROOT params:", ...) | ||
35 | local llthreads = require"llthreads" -- need to re-declare this under this scope | ||
36 | local t = {} -- thread storage table | ||
37 | |||
38 | -- create a new child sub-thread execution code - it requires level 1 literal string [=[ ]=] enclosures, level 2 would be [==[ ]==] | ||
39 | local executed_child_code = [=[ | ||
40 | return "Hello from child sub-thread, new input params:", ... | ||
41 | ]=] | ||
42 | |||
43 | -- create 1000 sub-threads - which creates an incremental 30% / 20% utilization spike on the two AMD cpu cores | ||
44 | print("CHILD: Create sub threads:", num_threads) | ||
45 | for i=1,num_threads do | ||
46 | -- create child sub-thread with code to execute and the input parmeters | ||
47 | local thread = llthreads.new(executed_child_code , "number:", 1000 + i, "nil:", nil, "bool:", true) | ||
48 | assert(thread:start()) -- start new child sub-thread | ||
49 | table.insert(t, thread) -- append the thread at the end of the thread table | ||
50 | end | ||
51 | |||
52 | -- wait (block) for all child sub-threads to complete before returning to ROOT | ||
53 | while true do | ||
54 | -- always wait on the first element, since order is not important | ||
55 | print("CHILD: sub-thread returned: ", t[1]:join()) | ||
56 | table.remove(t,1) -- always remove the first element | ||
57 | if (#t == 0) then break end | ||
58 | end | ||
59 | return ... -- return the parents' input params back to the root | ||
60 | ]] | ||
61 | |||
62 | -- create child thread. | ||
63 | local thread = llthreads.new(thread_code, num_threads, "number:", 1000, "nil:", nil, "bool:", true) | ||
64 | -- start joinable child thread. | ||
65 | assert(thread:start()) | ||
66 | -- wait for all child and child sub-threads to finish | ||
67 | print("ROOT: child returned: ", thread:join()) | ||