Skip to content

Instantly share code, notes, and snippets.

@simopal6
Created June 15, 2016 11:52
Show Gist options
  • Save simopal6/fe33bf98ec56da5a92e803ec018320a1 to your computer and use it in GitHub Desktop.
Save simopal6/fe33bf98ec56da5a92e803ec018320a1 to your computer and use it in GitHub Desktop.
Torch thread serialization of tables with custom metatables
-- Create table with metatable
tab = {}
idx = {a = 1}
setmetatable(tab, {__index = idx})
-- Setup threads
threads = require 'threads'
threads.Threads.serialization('threads.sharedserialize') -- Commenting this does not affect the outcome
pool = threads.Threads(2)
-- Prepare job
do
-- Setup upvalues
local tab_up = tab
local tab_mt_up = getmetatable(tab)
-- Add job
pool:addjob(function()
print("T1: tab_up.a (before setmetatable): " .. tostring(tab_up.a)) -- nil
setmetatable(tab_up, tab_mt_up)
print("T1: tab_up.a (after setmetatable): " .. tostring(tab_up.a)) -- 1
end)
end
-- Clean exit
pool:synchronize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment