Skip to main content

ThreadFnPtr

Type Alias ThreadFnPtr 

Source
pub type ThreadFnPtr = dyn Fn(Box<dyn Thread>, Option<ThreadParam>) -> Result<ThreadParam> + Send + Sync + 'static;
Expand description

All OSAL trait definitions for advanced usage. All OSAL trait definitions for advanced usage. All OSAL trait definitions for advanced usage. Thread callback function pointer type.

Thread callbacks receive a boxed thread handle and optional parameter, and can return an updated parameter value.

§Parameters

  • Box<dyn Thread> - Handle to the thread itself (for self-reference)
  • Option<ThreadParam> - Optional type-erased parameter passed at spawn time

§Returns

Result<ThreadParam> - Updated parameter or error

§Trait Bounds

The function must be Send + Sync + 'static to be safely used across thread boundaries and to live for the duration of the thread.

§Examples

use osal_rs::os::*;
use std::sync::Arc;

let callback: Box<ThreadFnPtr> = Box::new(|_thread, param| {
    if let Some(p) = param {
        if let Some(count) = p.downcast_ref::<u32>() {
            return Ok(Arc::new(*count + 1));
        }
    }
    Ok(Arc::new(0u32))
});

// This is how `spawn` invokes it: with a handle to the thread itself and
// the parameter it was spawned with.
let thread = Thread::new("worker", 1024, 1);
let result = callback(Box::new(thread), Some(Arc::new(41u32))).unwrap();

assert_eq!(result.downcast_ref::<u32>(), Some(&42));