For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt, and this page is available as Markdown at /next/api/lynx-native-api/lynx-extension-module/is-running-tasks-on-bts-thread.md.
Lynx
  • English
  • IsRunningTasksOnBTSThread

    The IsRunningTasksOnBTSThread method is a utility function that checks whether the current code is executing on the BTS (Background Thread Script) thread.

    This is crucial for thread safety. Before calling any N-API functions or accessing resources that are only safe to use on the BTS thread, you should use this check to prevent crashes and race conditions.

    Method Signature

    bool IsRunningTasksOnBTSThread();

    Return Value

    • true: If the current thread is the BTS thread.
    • false: If the current thread is not the BTS thread (e.g., it is the UI thread or another worker thread).

    Usage

    Use this method as a guard before performing BTS-thread-specific operations, especially in functions that could be called from multiple threads.

    Example

    #include "lynx_extension_module.h"
    
    class MyExtensionModule : public lynx::pub::LynxExtensionModule {
    private:
        napi_env _env = nullptr;
    
    public:
        void OnRuntimeAttach(
            Napi::Env env,
            std::unique_ptr<lynx::pub::VSyncObserver> /*vsync_observer*/) override {
            _env = env;
        }
    
        void SomeMethodThatCouldBeCalledFromAnyThread() {
            if (IsRunningTasksOnBTSThread()) {
                // Safe to call N-API functions directly
                printf("Running on BTS thread. Performing N-API call.\\n");
                // napi_do_something(_env, ...);
            } else {
                // Not on the BTS thread, so post a task
                printf("Not on BTS thread. Posting task instead.\\n");
                PostTaskToRuntime([this]() {
                    // Now we are on the BTS thread
                    // napi_do_something(_env, ...);
                });
            }
        }
    
        // ... other methods
    };

    See Also

    • PostTaskToRuntime(): The method to use when you are not on the BTS thread and need to execute code there.

    Compatibility

    LCD tables only load in the browser

    Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.