Skip to content

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

@suhaspillai

Description

@suhaspillai

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions