PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Sample implementation of CriticalSection class

ChezDoodles · 23 Apr 2006, 21:45 UTC
[edit]
SORRY - this was supposed to be a reply to the previous thread...
[/edit]


First the INTERFACE
===================

Code:
class iCriticalSection // Pure virtual interface...
{
public:
virtual bool Init( void ) = 0;
virtual void Close( void ) = 0;

virtual bool Enter( bool bFailIf ) = 0;
virtual void Leave( void ) = 0;
};



Then, a real CLASS DEFINITION (still platform independent)
==========================================================

Code:
class CriticalSection : public iCriticalSection
{
CriticalSection( iMemMgr* pMemMgr );
~CriticalSection( void );

public:
virtual bool Init( void );
virtual void Close( void );

virtual void Enter( void );
virtual void Leave( void );

protected:
iMemMgr* m_pMemMgr; // Any kind of mem-manager will do...
void* m_pCriticalSectionObj;
};




At last, a Win32 IMPLEMENTATION
===============================

Code:
CriticalSection::CriticalSection( iMemMgr* pMemMgr )
:
iCriticalSection()
{
m_pMemMgr = pMemMgr;
m_pCriticalSectionObj = NULL;
}

CriticalSection::~CriticalSection( void )
{
if( m_pCriticalSectionObj )
m_pMemMgr->Free( m_pCriticalSectionObj );
}

bool CriticalSection::Init( void )
{
if( !m_pCriticalSectionObj )
{
myAssert( m_pMemMgr );
m_pCriticalSectionObj = m_pMemMgr->Alloc( sizeof( CRITICAL_SECTION ) );
}
myAssert( m_pCriticalSectionObj );
::InitializeCriticalSection( ( CRITICAL_SECTION* )m_pCriticalSectionObj );
return true;
}


void CriticalSection::Close( void )
{
myAssert( m_pCriticalSectionObj );
::DeleteCriticalSection( ( CRITICAL_SECTION* )m_pCriticalSectionObj );
}


void CriticalSection::Enter( void )
{
myAssert( m_pCriticalSectionObj );
::EnterCriticalSection( ( CRITICAL_SECTION* )m_pCriticalSectionObj );
}


void CriticalSection::Leave( void )
{
myAssert( m_pCriticalSectionObj );
::LeaveCriticalSection( ( CRITICAL_SECTION* )m_pCriticalSectionObj );
}
No problem, too bad pocketmatrix doesn't have an option to merge threads.

Either way, I assume critical sections will be used to access global data within classes (so other threads accessing the same piece of code needs to wait until the critical section leaves)?

Thanks for the source code, it will help us implement your suggestions faster and more easily.
ChezDoodles · 24 Apr 2006, 08:13 UTC
You also really need Events, and preferably Semaphores. On Win32 these are just as easy to implement - I'll paste some source later. To do interprocess communication, you would need Mutex'es as well, but I recon multiple processes are not really an issue on handheld devices...

(unless you would like to communicate between a TODAY screen plugin on a Smartphone or PocketPC, and your game application...)

BUT - these objects are used to syncronise threads, so unless you implement support for launching threads.... :)

(I'll send you some source on that as well later this week)

PLEASE add a MOVE member function to your ClassEMemory - thanks.
Alright, that sounds nice. Can you tell me if the Symbian active objects can be compared to threads?