gre.thread create

gre.thread_create(func)
		    

This function starts a new operating system thread of execution that is independent from Storyboard's main thread. The function provided as an argument indicates the starting context for this new thread of execution.

The Storyboard data and event (get_data/set_data/send_event) API are thread safe. However the execution of data changes outside of the main thread of execution can have a significant impact on performance of the application and the preferred way of synchronizing data obtained in a thread with the Storyboard UI thread is by using a Storyboard IO event and sending the data via gre.send_event or gre.send_event_data. There are no thread specific synchronization primitives, such as mutexes, for synchronizing Lua data access, serialize to the main thread using an event if this is a requirement.

In scenarios where a controlled shutdown and restart of a Storyboard application is required, separate threads of execution pose a synchronization challenge. In these situations all created thread(s) must have their execution interrupted and terminate in order for a clean shutdown to be observed. This can be accomplished nominally by intercepting the gre.quit event and then taking appropriate action to flag a shutdown variable or send an unblocking event.

This function is not available on all systems and is not available if gre.thread_create is set to nil.

Parameters: func The Lua function to run in a separate thread of execution from the main UI thread. Example:

-- Flag to indicate that we want our threads to quit executing
local quit_threads = false

-- Run a poll loop waiting for a file (a_file) to appear and
-- then send an event
function async_function()
    while(not quit_threads) do
        local fp = io.open("a_file")
        if(fp ~= nil) then
            fp:close()
            gre.send_event("file_created")
            return
        end
    end
end

-- Create the monitoring thread of execution
gre.thread_create(async_function)
		    
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Article is closed for comments.