Threading Model

BrightScript runs in a single thread. In general, BrightScript object calls are synchronous if they return quickly, and asynchronous if they take a substantial amount of time to complete. For example, methods belonging to the roArray object are all synchronous, while the Play() method that is part of the roVideoPlayer object will return immediately (it is asynchronous). As a video plays, the roVideoPlayer object will post messages to the message port, indicating such events as “media playback finished” or “frame x reached”.                                  

The object implementer decides whether a BrightScript object should launch a background thread to perform a synchronous operation. Sometimes, an object will feature synchronous and asynchronous versions of the same method.

This threading model ensures that the script writer does not have to deal with mutexes and other synchronization objects. The script is always single threaded, and the message port is polled or waited on to receive events into the thread. On the other hand, those implementing BrightScript objects have to consider threading issues: For example, the roList and roMessagePort objects are thread-safe internally, allowing them to be used by multiple threads.