Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to share data across child and main threads using tables ? #90

Open
suhaspillai opened this issue Dec 24, 2016 · 0 comments
Open

How to share data across child and main threads using tables ? #90

suhaspillai opened this issue Dec 24, 2016 · 0 comments

Comments

@suhaspillai
Copy link

I have a table H_next and I do some calculation (call add()) and store value in H_next, which is shared across all the threads.

local function add(a,b)
    return (a+b)
end

function indexer:cal()
    local H_next={}
    local temp_H_next={}
    local count = 1
    for i = 1,100 do
        local val =10
        self.pool:addjob(function()
        H_next[i] = add(i,val+1)
        --print (H_next[i])
        return H_next
       end
      ,
      function(val)
          for i=1,#val do
          temp_H_next[count] = val[i]
          count  = count + 1
         end
        --temp_H_next[count] = val
        --[[H_next[count] = val
        count = count + 1--]]
        end)
    end

   self.pool:synchronize()
   self.pool:terminate()

  for i,v in pairs(temp_H_next) do
      print (i,v)
  end
end

This just prints 2 values

I am only able to get all the values when I modify the function as follows

local H_next={}
local temp_H_next={}
local count = 1
for i = 1,100 do
    local val =10
    self.pool:addjob(function()
    H_next[i] = add(i,val+1)
    --print (H_next[i])
    return H_next[i]   -- When I return one by one 
    end
  ,
  function(val)
    --[[for i=1,#val do
      temp_H_next[count] = val[i]
      count  = count + 1
    end--]]
    
    temp_H_next[count] = val
    count = count + 1
  end)
end

The problem with 2nd approach is that it takes a lot of time to communicate between child and main thread, I want to fill the H_next table for that particular thread then transfer those values or copy those values in main thread in temp_H_next using endcallback.
Is there a better way to do this, where I don't have to send one by one value at a time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant