<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Scriptionary Blog &#187; pthreads</title>
	<atom:link href="http://blog.scriptionary.com/tag/pthreads/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.scriptionary.com</link>
	<description>The informal yet informational sub-site</description>
	<lastBuildDate>Fri, 19 Feb 2010 16:21:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CriticalSection wrapper class</title>
		<link>http://blog.scriptionary.com/2008/08/22/criticalsection-wrapper-class/</link>
		<comments>http://blog.scriptionary.com/2008/08/22/criticalsection-wrapper-class/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 15:09:39 +0000</pubDate>
		<dc:creator>Eddy Luten</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[posix]]></category>
		<category><![CDATA[pthreads]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://blog.scriptionary.com/?p=41</guid>
		<description><![CDATA[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 &#60;windows.h&#62;
#else
#include &#60;unistd.h&#62;
#include &#60;pthread.h&#62;
#endif

/**
 * @class A wrapper-class around [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What:</strong> A C++ wrapper around both WINAPI (Microsoft Windows) and PThreads (POSIX threads) functionality.<br />
<strong>Why:</strong> To abstract cross platform functionality.<br />
<strong>Remarks:</strong> 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.<br />
<span id="more-41"></span></p>
<pre style="overflow: auto; height: 400px; background-color: #EFEFEF; margin: 5px; padding: 5px; border: 1px solid #CCC;">
#ifdef _WIN32
#include &lt;windows.h&gt;
#else
#include &lt;unistd.h&gt;
#include &lt;pthread.h&gt;
#endif

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

	/**
	 * @brief CriticalSection class destructor
	 */
	~CriticalSection(void)
	{
		this-&gt;WaitForFinish(); // acquire ownership
	#ifdef _WIN32
		DeleteCriticalSection(&#038;this-&gt;m_cSection);
	#else
		pthread_mutex_destroy(&#038;this-&gt;m_cSection);
	#endif
	}; // ~CriticalSection()

	/**
	 * @fn void WaitForFinish(void)
	 * @brief Waits for the critical section to unlock.
	 * This function puts the waiting thread in a waiting
	 * state.
	 * @see TryEnter()
	 * @return void
	 */
	void WaitForFinish(void)
	{
		while(!this-&gt;TryEnter())
		{
		#ifdef _WIN32
			Sleep(1); // put waiting thread to sleep for 1ms
		#else
			usleep(1000); // put waiting thread to sleep for 1ms (1000us)
		#endif
		};
	}; // WaitForFinish()

	/**
	 * @fn void Enter(void)
	 * @brief Wait for unlock and enter the CriticalSection object.
	 * @see TryEnter()
	 * @return void
	 */
	void Enter(void)
	{
		this-&gt;WaitForFinish(); // acquire ownership
	#ifdef _WIN32
		EnterCriticalSection(&#038;this-&gt;m_cSection);
	#else
		pthread_mutex_lock(&#038;this-&gt;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(&#038;this-&gt;m_cSection);
	#else
		pthread_mutex_unlock(&#038;this-&gt;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(&#038;this-&gt;m_cSection));
	#else
		return(0 == pthread_mutex_trylock(&#038;this-&gt;m_cSection));
	#endif
	}; // TryEnter()

private:
#ifdef _WIN32
	CRITICAL_SECTION m_cSection; //!&lt; internal system critical section object (windows)
#else
	pthread_mutex_t m_cSection; //!&lt; internal system critical section object (*nix)
#endif
}; // class CriticalSection
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.scriptionary.com/2008/08/22/criticalsection-wrapper-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
