task_rw_mutex - Melnytskyi/fast_task GitHub Wiki
class task_rw_mutex {
public:
task_rw_mutex() = default;
~task_rw_mutex();
void read_lock();
bool try_read_lock();
bool try_read_lock_for(size_t milliseconds);
bool try_read_lock_until(std::chrono::high_resolution_clock::time_point time_point);
void read_unlock();
bool is_read_locked();
void lifecycle_read_lock(std::shared_ptr<task>& task);
void write_lock();
bool try_write_lock();
bool try_write_lock_for(size_t milliseconds);
bool try_write_lock_until(std::chrono::high_resolution_clock::time_point time_point);
void write_unlock();
bool is_write_locked();
void lifecycle_write_lock(std::shared_ptr<task>& task);
bool is_own();
};
The task_rw_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. It allows multiple threads to read the data in shared mode but only one thread to write or modify the data in exclusive mode. This class is particularly useful in scenarios where data is read more often than it is written or modified.
void read_lock();
Acquires the read lock. If the write lock is already held by another thread, this call will block until the write lock is released.
bool try_read_lock();
Tries to acquire the read lock without blocking. Returns true if the lock was successfully acquired.
bool try_read_lock_for(size_t milliseconds);
Tries to acquire the read lock, blocking for a specified duration if the write lock is held by another thread. Returns true if the lock was acquired.
bool try_read_lock_until(std::chrono::high_resolution_clock::time_point time_point);
Tries to acquire the read lock, blocking until a specified time point if the write lock is held by another thread. Returns true if the lock was acquired.
void read_unlock();
Releases the read lock.
bool is_read_locked();
Checks if the read lock is currently held.
void write_lock();
Acquires the write lock. If the lock is held in any mode by another thread, this call will block until it can acquire the lock.
bool try_write_lock();
Tries to acquire the write lock without blocking. Returns true if the lock was successfully acquired.
bool try_write_lock_for(size_t milliseconds);
Tries to acquire the write lock, blocking for a specified duration if the lock is held by another thread. Returns true if the lock was acquired.
bool try_write_lock_until(std::chrono::high_resolution_clock::time_point time_point);
Tries to acquire the write lock, blocking until a specified time point if the lock is held by another thread. Returns true if the lock was acquired.
void write_unlock();
Releases the write lock.
bool is_write_locked();
Checks if the write lock is currently held.
void lifecycle_read_lock(const std::shared_ptr<task>& task);
Starts a task with the read lock, ensuring the lock is held for the lifecycle of the task.
void lifecycle_write_lock(const std::shared_ptr<task>& task);
Starts a task with the write lock, ensuring the lock is held for the lifecycle of the task.
bool is_own();
Checks if the current thread owns the lock.
void reader_function(task_rw_mutex& mutex) {
mutex.read_lock();
// Perform read operations...
mutex.read_unlock();
}
void writer_function(task_rw_mutex& mutex) {
mutex.write_lock();
// Perform write operations...
mutex.write_unlock();
}
int main() {
task_rw_mutex mutex;
std::thread reader(reader_function, std::ref(mutex));
std::thread writer(writer_function, std::ref(mutex));
reader.join();
writer.join();
return 0;
}