Struct more_sync::VersionedParker [−][src]
pub struct VersionedParker<T> { /* fields omitted */ }
Expand description
A thread parking and locking primitive that provide version numbers.
Like an std::sync::Condvar
, VersionedParker
provides a wait
method and several notify
methods. The wait
method blocks the current
thread, while the notify
methods unblocks waiting threads. Each time
notify
is called, the parker version is increased. When a blocked thread
wakes up, it can check the internal counter and learn how many times it has
been notified. The version can be obtained by calling method
VersionedParker::version()
.
VersionedParker
holds a piece of data that can be modified during notify
and wait
operations. The data is versioned also versioned by the same
parker version.
use more_sync::VersionedParker;
let versioned_parker = VersionedParker::new(0);
let mut guard = versioned_parker.lock();
let parker_clone = versioned_parker.clone();
std::thread::spawn(move || {
parker_clone.notify_one_mutate(|i| *i = 16);
assert_eq!(parker_clone.version(), 1);
// Version is 1, try_notify_all() should fail.
assert!(!parker_clone.try_notify_all(0));
});
guard.wait();
assert_eq!(guard.notified_count(), 1);
assert_eq!(*guard, 16);
Implementations
Creates a new VersionedParker
, with the initial version being 0
, and
the shared data being data
.
Locks the shared data and the version.
A thread can then call VersionedGuard::wait()
to wait for version
changes.
Increases the version and notifies one blocked thread.
Increases the version, mutates the shared data and notifies one blocked thread.
Increases the version and notifies one blocked thread, if the current
version is expected_version
.
Returns true
if the version matches.
Increases the version, modifies the shared data and notifies one blocked
thread, if the current version is expected_version
.
Returns true
if the version matches.
Increases the version and notifies all blocked threads.
Increases the version, modifies the shared data and notifies all blocked threads.
Increases the version and notifies all blocked threads, if the current
version is expected_version
.
Returns true
if the version matches.
Increases the version, modifies the shared data and notifies all blocked
threads, if the current version is expected_version
.
Returns true
if the version matches.
Trait Implementations
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl<T> !RefUnwindSafe for VersionedParker<T>
impl<T> Send for VersionedParker<T> where
T: Send,
impl<T> Sync for VersionedParker<T> where
T: Send,
impl<T> Unpin for VersionedParker<T>
impl<T> !UnwindSafe for VersionedParker<T>
Blanket Implementations
Mutably borrows from an owned value. Read more