Click here to Skip to main content
15,895,709 members
Articles / Programming Languages / C++

CRWCriticalSection : Another solution to the readers/writers problem with time-out

Rate me:
Please Sign up or sign in to vote.
4.63/5 (24 votes)
27 Oct 2003CPOL6 min read 81.1K   2.1K   32  
A class for synchronizing reader and writer threads
// RWCriticalSection.h: interface for the CRWCriticalSection class.
//
//////////////////////////////////////////////////////////////////////
// (c) Jerome Sopocko 2003
// this code worked last time I saw it
//////////////////////////////////////////////////////////////////////
// v1.0 27/10/2003	First release
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_RWCRITICALSECTION_H__A0994F02_8CE5_45CB_975C_C0D1D5F93BF8__INCLUDED_)
#define AFX_RWCRITICALSECTION_H__A0994F02_8CE5_45CB_975C_C0D1D5F93BF8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CRWCriticalSection  
{
public:
	CRWCriticalSection();
	virtual ~CRWCriticalSection();
protected:
	HANDLE m_hNobodyIsReading;
	HANDLE m_hWritingMutex;
	CRITICAL_SECTION m_csReading;
	int m_nReaders;
public:
	bool LockReader(DWORD dwTimeOut=INFINITE);
	void UnlockReader();
	bool LockWriter(DWORD dwTimeOut=INFINITE);
	void UnlockWriter();

};

#endif // !defined(AFX_RWCRITICALSECTION_H__A0994F02_8CE5_45CB_975C_C0D1D5F93BF8__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
Known as "The Wandering Geek", I have had to often change identities and countries due to the low quality level of the numerous software I have left behind.
Never wrote a software that did more than sorting 3 numbers which actually worked.
Hey but feel free to download my stuff!


Comments and Discussions