Crate threadcell

source ·
Expand description

A cell whose value can only be accessed by a owning thread. Much like a Mutex but without blocking locks. Access to ThreadCells is passed cooperatively between threads.

Semantics

ThreadCell

A ThreadCell and references therof can always be send to other threads

  • A ThreadCell that is owned by a thread then only that thread can:
    • Access its value
    • Drop the cell.
    • Set the Cell into a disowned state.
  • On a ThreadCell that is disowned any thread can:
    • Take ownership of it
    • Drop it

Threads that do not own a ThreadCell and access its value will panic. There are ‘try_*’ variants in the API that will not panic but return a bool or Option instead.

Guard

threadcell::Guard is optional and (only) handle proper cleanup when it becomes dropped. Unlike MutexGuards they are explicitly constructed and may not hold the threads ownership of the referenced ThreadCell.

Use Cases

  • Single threaded applications that need a static mutable global variable can use ThreadCell<RefCell<T>>.
  • Sharing data between threads where synchronizaton is done out of band with other syncronization primitives.

Structs

Guards that a referenced ThreadCell becomes properly released when its guard becomes dropped. This should cover most panics that do not end in an abort() as well. There are some cases where panics can escape this, for example when one registers custom panic handlers.
A cell that can be owned by a single thread or none at all.