Created
June 22, 2016 12:11
-
-
Save albanD/026712d698973c2f190aa7f3940e86a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local threads = require "threads" | |
threads.Threads.serialization('threads.sharedserialize') | |
n_task = 3 | |
local pools = {} | |
for task=1,n_task do | |
pools[task] = threads.Threads(5, | |
function() | |
-- Needed only for serialized elements | |
-- The others can be required in a local | |
-- in the job | |
end | |
) | |
end | |
local endcallback | |
local finish_task | |
-- Scope the callback | |
do | |
-- Variable to get back the returned value | |
local returned_value | |
-- The endcallback for the tasks | |
endcallback = function(work_result) | |
returned_value = work_result | |
end | |
-- The function to get the returned value | |
finish_task = function(task) | |
pools[task]:dojob() | |
return returned_value | |
end | |
end | |
function start_task(task) | |
local task = task | |
print("M" .. task .. ": adding job") | |
pools[task]:addjob( | |
function() | |
local sys = require "sys" | |
local threads = require "threads" | |
local torch = require "torch" | |
print("T" .. task .. ": started") | |
-- Work | |
local work_time = torch.random(1,2) | |
sys.sleep(work_time) | |
local work_result = torch.random(1,100) | |
print("T" .. task .. ": result of work " .. work_result) | |
return work_result | |
end, | |
endcallback | |
) | |
end | |
-- Initially start all tasks | |
start_task(1) | |
start_task(2) | |
start_task(3) | |
-- Start main loop | |
while true do | |
-- Get random task | |
local task = torch.random(1, 3) | |
print("M" .. task .. ": finishing task") | |
local result = finish_task(task) | |
print("M" .. task .. ": result is " .. result) | |
print("M" .. task .. ": restarting task") | |
start_task(task) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment