Skip to content

CPP Library Critical_Section

Jeongho Nam edited this page Aug 16, 2016 · 2 revisions
#include <exception>
#include <string>

#include <samchon/library/RWMutex.hpp>
#include <samchon/library/Semaphore.hpp>

using namespace std;
using namespace samchon::library;

class Member
{
private:
	string id;
	string name;
	int age;
	
	RWMutex mtx;
	Semaphore semaphore;
	
public:
	auto getID() const -> string
	{
		mtx.readLock();
		string id;
		mtx.readUnlock();
		
		return id;
	};
	
	void setID(const string &val)
	{
		mtx.writeLock();
		this->id = val;
		mtx.writeUnlock();
	}

	auto getName() const -> string
	{
		UniqueReadLock uk(mtx);
		return name;
	};
	
	void setName(const string &val)
	{
		UniqueWriteLock uk(mtx);
		this->name = val;
	};
	
	void invite()
	{
		UniqueAcquire ua(semaphore);
		
		// DO SOMETHING
		throw exception("dummy exception");
	};
};