pub struct Ticket<T> {
serving: AtomicUsize,
ticket: AtomicUsize,
value: UnsafeCell<T>,
}
Expand description
By the use of Ticketlocks, it is possible to serialize blocks of code that might run parallel on multiple CPU cores.
Synchronization is implemented using a lock and a ticket variable. Once a thread tries to enter the critical area, it obtains a ticket by atomic increment of the ticket variable and waits until the lock variable is equal to its ticket. When a thread leaves the critical area, it increments the lock variable by one and thereby allows the next thread to enter the critical area.
Fields§
§serving: AtomicUsize
§ticket: AtomicUsize
§value: UnsafeCell<T>
Wrap the mutable data inside an unsafe cell, so that the compiler doesn’t optimize it away
Implementations§
source§impl<T> Ticket<T>
impl<T> Ticket<T>
sourcepub fn lock(&self) -> TicketGuard<'_, T>
pub fn lock(&self) -> TicketGuard<'_, T>
Locks the lock and returns the protected data.
The data is wrapped in a guard, that unlocks the lock if it goes out of scope.