Skip to main content

with_r_thread

Function with_r_thread 

Source
pub fn with_r_thread<F, R>(f: F) -> R
where F: FnOnce() -> R + 'static, R: Send + 'static,
Expand description

Execute a closure on R’s main thread, returning the result.

This function can be called from any thread:

  • From the main thread: executes the closure directly (re-entrant)
  • From the worker thread (during run_on_worker): sends the work to the main thread and blocks until completion

§Panics

  • If miniextendr_runtime_init() hasn’t been called yet
  • If called from a non-main thread without the worker-thread feature
  • If called from a non-main thread outside of a run_on_worker context (even with the worker-thread feature)

§Example

use miniextendr_api::with_r_thread;

// From worker thread, safely call R APIs:
let sexp = with_r_thread(|| {
    // This runs on R's main thread
    SEXP::nil()
});