Skip to content

CefThread

Header: cef_thread.h
Category: System Utilities

Overview

Tasks, threads, parsers, streams, cookies, URL requests, scheme registration, and constant tables.

CefThread

Source File: include/cef_thread.h

Process / Thread Context: Any valid CEF thread in either the browser or render process can call CreateThread. Lifecycle methods (Stop, IsRunning) must be called from the same thread that called CreateThread. GetTaskRunner / GetPlatformThreadId are safe from any thread.

Purpose: Convenience wrapper that spawns a new OS thread running a CEF message loop. Most use cases should prefer posting to an existing CEF thread rather than creating one; reserve CefThread for workloads that genuinely need a dedicated thread (custom IO, COM apartments, etc.).

Methods

static CefRefPtr<CefThread> CreateThread(const CefString& display_name, cef_thread_priority_t priority, cef_message_loop_type_t message_loop_type, bool stoppable, cef_com_init_mode_t com_init_mode)

Parameters:

  • display_name: human-readable name for diagnostics (optional_param — empty allowed).
  • priority: TP_BACKGROUND, TP_NORMAL, TP_DISPLAY, TP_REALTIME_AUDIO.
  • message_loop_type: ML_TYPE_DEFAULT (tasks + timers), ML_TYPE_UI (tasks + timers + native UI events), ML_TYPE_IO (tasks + timers + async IO).
  • stoppable: true → thread can be stopped and joined on destruction / Stop(); false → thread leaks on shutdown (cannot Stop()).

Return Value: New thread (does not block for init). nullptr on failure.

Threading Constraint: Any valid CEF thread (browser or render).

static CefRefPtr<CefThread> CreateThread(const CefString& display_name)

Return Value: convenience overload using TP_NORMAL, ML_TYPE_DEFAULT, stoppable=true, COM_INIT_MODE_NONE.

Threading Constraint: Threading constraint not specified in source.

virtual CefRefPtr<CefTaskRunner> GetTaskRunner()

Parameters:

  • (none)

Return Value: Task runner that posts tasks to this thread's message loop.

Threading Constraint: Safe from any thread.

virtual cef_platform_thread_id_t GetPlatformThreadId()

Parameters:

  • (none)

Return Value: OS-level thread ID (DWORD on Windows, pid_t on POSIX). Same value after Stop().

Threading Constraint: Safe from any thread.

virtual void Stop()

Parameters:

  • (none)

Return Value: (none)

Usage Instruction: Stop and join. All pending tasks run to completion first. Must be called from the same thread that called CreateThread. Must NOT be called if stoppable=false.

Threading Constraint: Creator thread.

virtual bool IsRunning()

Parameters:

  • (none)

Return Value: true if the thread is currently running.

Threading Constraint: Creator thread.

Usage Example

cpp
// Dedicated background IO thread for our app.
class App {
 public:
  void Start() {
    worker_ = CefThread::CreateThread("AppWorker");
    // Post tasks via worker_->GetTaskRunner()->PostTask(...)
  }
  void Shutdown() {
    if (worker_) worker_->Stop();  // blocks until pending tasks drain
    worker_ = nullptr;
  }
 private:
  CefRefPtr<CefThread> worker_;
};

// Windows COM STA thread:
CefRefPtr<CefThread> sta = CefThread::CreateThread(
    "ComStaWorker", TP_NORMAL, ML_TYPE_UI, /*stoppable=*/true,
    COM_INIT_MODE_STA);

Derived from the CEF C++ headers — © Marshall A. Greenblatt, Google Inc. & contributors (BSD-style license). Not an official CEF project.