sync
Synchronization primitives. Cross-platform: pthread primitives on POSIX, the Win32 equivalents (SRWLOCK, condition variables) on Windows, selected by target.
public module sync::atomic;
public module sync::mutex;
public module sync::rwlock;
public module sync::condvar;
public module sync::once;
public module sync::barrier;
public module sync::mpsc;
| File | What it holds | Import |
|---|---|---|
atomic | Atomic<T>, MemoryOrder, fence | import std::sync::atomic; |
mutex | Mutex<T, A>, MutexGuard | import std::sync::mutex; |
rwlock | RwLock<T, A> and its guards | import std::sync::rwlock; |
condvar | CondVar | import std::sync::condvar; |
once | Once | import std::sync::once; |
barrier | Barrier | import std::sync::barrier; |
mpsc | channel, Sender, Receiver | import std::sync::mpsc; |
Sharing any of these across threads means wrapping them in Arc. The guard types are !Send by design — a lock guard must be released by the thread that took it. See Send and Sync.
Every lock-like type here follows the same shape: a heap-allocated *Inner holding the platform primitive's raw bytes (pthread_buf) beside the guarded value, and a handle that is just a pointer to it. The pthread object's address therefore stays stable while other threads hold or wait on it — moving a pthread mutex after init is undefined behaviour — while the handle itself moves freely.
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it.
public module collections::array;In a directory's _module.cryo aggregator, lists a submodule and whether it is public. Importing the directory's namespace sees only the modules declared public here.
namespace std::sync::condvar;CondVar: condition variable for blocking until a predicate holds.
module pagetype struct Atomic<T> {
value: T;
}A single atomic cell of type T. See the module docs for the supported T and the compile-time dispatch model.
type enum MemoryOrder : u32 {
Relaxed = 2;
Acquire = 4;
Release = 5;
AcqRel = 6;
SeqCst = 7;
}Memory-ordering for an atomic operation.
std::sync::atomicimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct Mutex<T, A = GlobalAlloc> where A: Allocator {
inner: MutexInner<T, A>*;
}Mutex<T>: mutual-exclusion lock around a value.
std::sync::mutextype struct MutexGuard<T, A> {
inner: MutexInner<T, A>*;
}RAII guard returned by Mutex::lock. Carries a raw pointer to the inner so it can release the lock on drop. Deref via as_ptr() to read or mutate the guarded value.
type struct RwLock<T, A = GlobalAlloc> where A: Allocator {
inner: RwLockInner<T, A>*;
}RwLock<T>: read/write reader-writer lock around a value.
std::sync::rwlocktype struct CondVar {
inner: CondVarInner*;
}CondVar: condition variable for blocking until a predicate holds.
std::sync::condvartype struct Once {
inner: OnceInner*;
}Once: one-shot initialization synchronization.
std::sync::oncetype struct Sender<T> {
inner: ChannelInner<T>*;
}The sending half of a channel. Cloneable for multiple producers.
std::sync::mpsctype struct Receiver<T> {
inner: ChannelInner<T>*;
}The receiving half of a channel, and the channel's owner. Not cloneable (single consumer); mint producers with sender().
type trait Send {}Types that are safe to transfer between threads. An owning value of a Send type may cross a thread boundary without violating memory safety.