A way of reading and writing to a part of memory which is threadsafe.
An RwLock supports one writer, or multiple readers at once.
When a write lock is requested, all subsequent read locks will be blocked until the write lock succeeds.
struct Data {
int number;
bool boolean;
};
int main() {
{
RwLock<Data> lock;
/* Reading data */
int number = lock.read()->number;
/* Writing data */
lock.write()->number = 42;
}
{
RwLock<int> lock;
/* Reading data directly */
int number = lock.read().data();
/* Writing data directly */
lock.write().data() = 42;
}
return 0;
}
Written in 2023 by Max for the URE 17 racecar