CriticalSection wrapper class

What: A C++ wrapper around both WINAPI (Microsoft Windows) and PThreads (POSIX threads) functionality.
Why: To abstract cross platform functionality.
Remarks: On windows, CRITICAL_SECTION objects cannot be shared cross-process. This means that the class is tied to your application or DLL process. Comments are in Doxygen/Javadoc style.

#ifdef _WIN32
#	include <windows.h>
#else
#	include <unistd.h>
#	include <pthread.h>
#endif

/**
 * @class A wrapper-class around Critical Section functionality, WIN32 & PTHREADS.
 */
class CriticalSection
{
public:
	/**
	 * @brief CriticalSection class constructor.
	 */
	explicit CriticalSection(void)
	{
	#ifdef _WIN32
		if (0 == InitializeCriticalSectionAndSpinCount(&m_cSection, 0))
			throw("Could not create a CriticalSection");
	#else
		if (pthread_mutex_init(&m_cSection, NULL) != 0)
			throw("Could not create a CriticalSection");
	#endif
	}; // CriticalSection()

	/**
	 * @brief CriticalSection class destructor
	 */
	~CriticalSection(void)
	{
		Enter(); // acquire ownership (for pthread)
	#ifdef _WIN32
		DeleteCriticalSection(&m_cSection);
	#else
		pthread_mutex_destroy(&m_cSection);
	#endif
	}; // ~CriticalSection()

	/**
	 * @fn void Enter(void)
	 * @brief Wait for unlock and enter the CriticalSection object.
	 * @see TryEnter()
	 * @return void
	 */
	void Enter(void)
	{
	#ifdef _WIN32
		EnterCriticalSection(&m_cSection);
	#else
		pthread_mutex_lock(&m_cSection);
	#endif
	}; // Enter()

	/**
	 * @fn void Leave(void)
	 * @brief Leaves the critical section object.
	 * This function will only work if the current thread
	 * holds the current lock on the CriticalSection object
	 * called by Enter()
	 * @see Enter()
	 * @return void
	 */
	void Leave(void)
	{
	#ifdef _WIN32
		LeaveCriticalSection(&m_cSection);
	#else
		pthread_mutex_unlock(&m_cSection);
	#endif
	}; // Leave()

	/**
	 * @fn bool TryEnter(void)
	 * @brief Attempt to enter the CriticalSection object
	 * @return bool(true) on success, bool(false) if otherwise
	 */
	bool TryEnter(void)
	{
		// Attempt to acquire ownership:
	#ifdef _WIN32
		return(TRUE == TryEnterCriticalSection(&m_cSection));
	#else
		return(0 == pthread_mutex_trylock(&m_cSection));
	#endif
	}; // TryEnter()

private:
#ifdef _WIN32
	CRITICAL_SECTION m_cSection; //!< internal system critical section object (windows)
#else
	pthread_mutex_t m_cSection; //!< internal system critical section object (*nix)
#endif
}; // class CriticalSection

Comments

#1
On January 22nd, 2009 at 4:05 PM

[...] public links >> pthread CriticalSection wrapper class First saved by gferris | 13 days ago Installing Buildbot First saved by Imaxination80 | 86 [...]

#2
On February 1st, 2009 at 5:19 PM

[...] PHP – Recuperare le coordinate geografiche di un luogo Saved by xjulycutiex on Thu 22-1-2009 CriticalSection wrapper class Saved by pankajnagar on Tue 20-1-2009 IPbox 9000 Internal Cardreader Problem Saved by kmccravy on [...]

#3
Andrew Says:
On January 27th, 2011 at 8:55 AM

Is it ok with making thread sleep in WaitForFinish()? Does the function guarantee that proper thread is made sleep?

#4
On January 27th, 2011 at 9:31 AM

Hi Andrew,

No, it does not guarantee that. I’m actually removing the member function from the class since when I created it I didn’t put too much thought into the issue. The only way you could use that member function properly is by ensuring that it is called from the original/calling thread.

Thanks for the comment,
Eddy

Leave a Comment