Struct rayon_tlsctx::ThreadLocalCtx [−][src]
pub struct ThreadLocalCtx<T, F> { /* fields omitted */ }
Expand description
A thread local storage container for Rayon jobs
This context can be used to efficiently clone inner
, only when it’s necessary.
Implementations
impl<T, F: Fn() -> T> ThreadLocalCtx<T, F>
[src]
impl<T, F: Fn() -> T> ThreadLocalCtx<T, F>
[src]pub fn new(inner: F) -> Self
[src]
pub fn new(inner: F) -> Self
[src]Create a new TlsCtx
Examples
Creating a thread-local byte buffer:
use rayon_tlsctx::ThreadLocalCtx; let ctx = ThreadLocalCtx::new(Vec::<u8>::new);
pub fn new_locked(inner: F) -> ThreadLocalCtx<T, impl Fn() -> T>
[src]
pub fn new_locked(inner: F) -> ThreadLocalCtx<T, impl Fn() -> T>
[src]Create a new TlsCtx
.
This context utilises a lock for cloning values, making it usable for non-sync types.
Cloning an initialised buffer for each thread:
use rayon_tlsctx::ThreadLocalCtx; use rayon::iter::*; use std::cell::Cell; let mut buf: Vec<u16> = (0..!0).collect(); buf.shuffle(&mut rng); let buf = (buf, Cell::new(0)); // Must use new_locked, because cloning a cell across threads is not allowed let ctx = ThreadLocalCtx::new_locked(move || buf.clone()); (0..16).into_par_iter().for_each(|_| unsafe { ctx.get(); });
pub unsafe fn get<'a>(&'a self) -> ThreadLocalMut<'a, T, F>
[src]
pub unsafe fn get<'a>(&'a self) -> ThreadLocalMut<'a, T, F>
[src]Get a thread local context reference with dynamically checked borrow rules
Safety
Only one thread pool at a given time should use this scope. The thread pool size can not grow midway through.
Panics
If called from outside of a Rayon thread pool, or when the reference is already being borrowed.
Examples
use rayon_tlsctx::ThreadLocalCtx; use rayon::iter::*; const NUM_COPIES: usize = 16; let mut buf: Vec<u16> = (0..!0).collect(); // Create a thread local context with value 0. let ctx = ThreadLocalCtx::new(|| 0); // Sum the buffer `NUM_COPIES` times and accumulate the results // into the threaded pool of counts. Note that the counts may be // Unevenly distributed. (0..NUM_COPIES) .into_par_iter() .flat_map(|_| buf.par_iter()) .for_each(|i| { let mut cnt = unsafe { ctx.get() }; *cnt += *i as usize; }); let buf_sum = buf.into_iter().fold(0, |acc, i| acc + i as usize); // What matters is that the final sum matches the expected value. assert_eq!(ctx.into_iter().sum::<usize>(), buf_sum * NUM_COPIES);
Trait Implementations
impl<T, F> IntoIterator for ThreadLocalCtx<T, F>
[src]
impl<T, F> IntoIterator for ThreadLocalCtx<T, F>
[src]Consume the context and retrieve all created items.
impl<T, F: Send + Sync> Send for ThreadLocalCtx<T, F>
[src]
impl<T, F: Send + Sync> Sync for ThreadLocalCtx<T, F>
[src]
Auto Trait Implementations
impl<T, F> !RefUnwindSafe for ThreadLocalCtx<T, F>
impl<T, F> Unpin for ThreadLocalCtx<T, F> where
F: Unpin,
T: Unpin,
F: Unpin,
T: Unpin,
impl<T, F> UnwindSafe for ThreadLocalCtx<T, F> where
F: UnwindSafe,
T: RefUnwindSafe,
F: UnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> Pointable for T
impl<T> Pointable for T