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.

Returns the current version.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.