Question on Thread_Create

Hi, I am new to using the thread create function and ran into a question on how it operated from the lua context. My main question is, is there a way to setup a second event queue between the child and parent thread?

I am trying to move a portion of code that is cpu intensive over to it's own thread, however some data needs to be shared. So I tried something akin to :

return function(function_to_create_module, parent_data)
  local function setup_parent()
    gre.add_event_listener('child_write', function(args)
      parent_data.update(args.data)
    end)

  gre.add_event_listener('child_read', function(args)
    gre.send_event_data('parent_write', '1s0 data', serialize(request_data)
    end)

    parent_data_change(function(data)
      gre.send_event_data('parent_write', '1s0 data', serialized(data))
    end)
  end

  local function setup_child()
    local child_data = data_container()

    gre.add_event_listener('parent_write', function(args)
      child_data.update(args.data)
    end)

    child_data_change(function(data)
      gre.send_event_data('child_write', '1s0 data', serialized(data))
    end)

    function_to_create_threaded_module(child_data)
  end

  setup_parent()
  gre.thread_create(setup_child)
end

I had a data change from the parent trigger an infinite while loop in the child. This blocked the application, which I didn't really expect. My assumption is that the current event queue is synchronous, and since the listener is never resolved, the next event is not being handled. Does this sound reasonable? I tried manipulating the greio portion of the `sbengine` call with and without `queue`, but that did not seem to have the any effect.

It seems like I will need to start a separate application, which uses gre.io to open a non-blocking event queue. However, I figured I would ask here if there was an in lua way to spawn an event queue.

Also minor question from the documentation does create_thread make a coroutine or spawn a new thread?

0

Comments

2 comments
  • Hi Sean, 

    Your assumption is correct, event handlers are run on a single thread regardless of what thread registered them. So any handlers that are blocking, like your data change, will block everything else as well. 

    A way for you to achieve this would be to use gre.receive_event() which allows you to listen on a Storyboard IO channel. Listen for events in a while loop using gre.receive_event() in a new thread. As events are received they will be processed on the secondary thread unlike with gre.add_event_listener() which are guaranteed to run on the Storyboard main thread. Try this out and let us know if you have any questions.

    Also, gre.thread_create() will create new threads, not coroutines. 

    0
  • Hi Sarah,

      Thanks, we went a different way. However, thanks for the information!

    0

Please sign in to leave a comment.

Didn't find what you were looking for?

New post