|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article shows how to code a synchronized(myMutex)
{
//TODO put synchronized code here
}
The Mutex classThe following piece of code presents a mutex class with lock/unlock semantics (common in many libraries): //mutex class class Mutex { public: //the default constructor Mutex() { InitializeCriticalSection(&m_criticalSection); } //destructor ~Mutex() { DeleteCriticalSection(&m_criticalSection); } //lock void lock() { EnterCriticalSection(&m_criticalSection); } //unlock void unlock() { LeaveCriticalSection(&m_criticalSection); } private: CRITICAL_SECTION m_criticalSection; }; There is nothing particularly special for the above class:
We are going to use critical sections, but any synchronization primitive applies. The Lock classIn order to be consistent with the C++ established code practices, we need a special class for implementing the RAII (Resource Acquisition Is Initialisation) pattern. The following piece of code shows such a class: //synchronization controller object class Lock { public: //the default constructor Lock(Mutex &mutex) : m_mutex(mutex), m_locked(true) { mutex.lock(); } //the destructor ~Lock() { m_mutex.unlock(); } //report the state of locking when used as a boolean operator bool () const { return m_locked; } //unlock void setUnlock() { m_locked = false; } private: Mutex &m_mutex; bool m_locked; }; Things to note for this class:
Using the above class is pretty straightforward: Mutex mutex1; ... Lock lock1(mutex1); //synchronized code here The "synchronized" macroThe #define synchronized(M) for(Lock M##_lock = M; M##_lock; M##_lock.setUnlock()) where, the parameter Example of using the "synchronized" macroThe following piece of code shows how to use the synchronized macro: it coordinates two threads that print the alphabet in the standard output. Without synchronization, the output is not correct: //thread count int thread_count = 0; //mutex Mutex mutex1; //example thread DWORD CALLBACK thread_proc(LPVOID params) { for(int i = 0; i < 10; ++i) { synchronized(mutex1) { for(char c = 'A'; c <= 'Z'; ++c) { cout << c; } cout << endl; } } thread_count--; return 0; } //main int main() { thread_count = 2; CreateThread(0, 0, thread_proc, 0, 0, 0); CreateThread(0, 0, thread_proc, 0, 0, 0); while (thread_count) Sleep(0); getchar(); return 0; } How it worksThe macro exploits the nature of the
Advantages over classic RAIIUsing this way to code RAII has some advantages over the traditional method:
NotesThe
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||