StuBS
|
Using Ticketlocks, it is possible to serialize blocks of code that might otherwise run in parallel on multiple CPU cores, or be interleaved due to interrupts or scheduling. More...
#include <ticketlock.h>
Public Member Functions | |
Ticketlock () | |
Constructor. | |
void | lock () |
Enters the critical area. In case the area is already locked, lock() will actively wait until the area can be entered. | |
void | unlock () |
Unblocks the critical area. | |
Using Ticketlocks, it is possible to serialize blocks of code that might otherwise run in parallel on multiple CPU cores, or be interleaved due to interrupts or scheduling.
Synchronization is implemented using a lock and a ticket variable. Once a thread tries to enter the critical area, it obtains a ticket by atomically incrementing the ticket variable and waiting until the lock counter reaches this ticket, if it is not there already. 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.
If you want that things just work, choose __ATOMIC_SEQ_CST as memorder. This is not the most efficient memory order but works reasonably well.