Skip to content

Instantly share code, notes, and snippets.

@Preetam
Created October 4, 2015 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Preetam/80235b95f32a2a61e4fd to your computer and use it in GitHub Desktop.
Save Preetam/80235b95f32a2a61e4fd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <chrono>
#include "rwmutex.hpp"
cpl::RWMutex mu;
int
main() {
std::thread a([]() {
mu.r_lock();
std::cout << "Reader a" << std::endl;
mu.r_unlock();
});
std::thread b([]() {
mu.lock();
std::cout << "Writer b" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
mu.unlock();
});
std::thread c([]() {
mu.r_lock();
std::cout << "Reader c" << std::endl;
mu.r_unlock();
});
std::thread d([]() {
mu.r_lock();
std::cout << "Reader d" << std::endl;
mu.r_unlock();
});
std::thread e([]() {
mu.r_lock();
std::cout << "Reader e" << std::endl;
mu.r_unlock();
});
std::thread f([]() {
mu.lock();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Writer f" << std::endl;
mu.unlock();
});
a.join();
b.join();
c.join();
d.join();
e.join();
f.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment