NativeThread

interface NativeThread extends HybridObject

Represents a native "Thread".

A NativeThread is a JS handle to a native Thread implementation (most commonly, a DispatchQueue on iOS and an Executor on Android).

It can be used to create Worklet Runtimes that run on the given NativeThread.

Discussion

You typically do not create instances of NativeThread yourself, and shouldn't interact with it. Higher level APIs like the useFrameOutput(...) hook already create a JS Worklet Runtime for you.

Discussion

Some CameraOutputs (like the CameraFrameOutput) expose their NativeThread (see CameraFrameOutput.thread) so a JS Worklet Runtime can be created for that NativeThread to ensure all work is executed on the same Thread.

This is relevant for streaming outputs like the CameraFrameOutput to run synchronous JS functions (like a "Frame Processor") on the same Thread the output is running on, to prevent any Thread hops or synchronization - work can execute fully isolated and uninterrupted from other Runtimes.

Discussion

A NativeThread does not guarantee that it uses a single OS Thread. Instead, it may use a Thread Pool. Do not use static thread_local caches in C++, as this may break with pooled NativeThreads. The same concept applies to GCD (DispatchQueue) anyways.

Even though the NativeThread may use a Thread pool, it never runs code in parallel - execution is guaranteed to be serial.

Properties

id

readonly id: string

Get this Thread's ID.

Methods

runOnThread()

runOnThread(task: () => void): void
  • When called from JS, nothing changes - this just delays the call.
  • When called from native, this actually runs the task on this Thread.