Critical Section / Mutext questions.
I've read several posts and articles on Critical Sections and Mutex but i'm not sure how to go about useing them.
Most of the things i've read show another class wrapping a CRITICAL_SECTION object or a mutex object? Why is that?
I have a MFC dialog applicaiton and a direct rendering window. The MFC DLG class starts a new thread like so:
static unsigned long __stdcall StartRenderer(void * Renderer) {
((classA *)Renderer)->update();
return 0;
unsigned long id;
m_RenderThread = CreateThread(NULL, 4 * 1024 * 1024, StartRenderer, (void *)&m_Render, 0, &id);
AssertVerify(SetThreadPriority(m_RenderThread,THREAD_PRIORITY_HIGHEST));
}
So this starts another thread and the function update is now running on that thread. This update function is in classA...
In this update() function i acess some data like stl maps and strings that are also accessed from other funcitons in class A that get called and are running on the main thread.
After reading up on the issue i think the best solution would be to use Critical Sections. I'd call EnterCriticalSection on any function that is going to be touching data that needs to be shared and LeaveCriticalSection at the end of the functions.
I pretty much already tried doing this only with bools. Any function running on the main thread i'd set a bool at the start and bool at the end. and wouldn't go into a function running on the other thread to enter if the bool was set.. Still getting crashes trying this
So i'm not exactly sure how to set up the Critical Section. Where would i have to delcare and init the CRITICAL_SECTION so both threads are able to know about it?
[1764 byte] By [
messin18] at [2007-11-20 0:41:37]

# 2 Re: Critical Section / Mutext questions.
Yeah that's one of the things i read as well as part 2.
I'm still not clear on what the best solution for me is, Why things like CRITICAL_SECTION are wrapped, why not use CCriticalSection?, Where i need to declare it?
Some articles have showed examples where the Critical Section object is a global decleration and some have shown them declared within data classes...
edit:
Oh i didn't see there was also source code files i'll download them and look at em.
# 3 Re: Critical Section / Mutext questions.
I'm still not clear on what the best solution for me is, Why things like CRITICAL_SECTION are wrapped, why not use CCriticalSection?, Where i need to declare it?The best solution depends on your requirements. If theres only one process accessing the 'protected resource', then a critical section is usually enough. If you need synchronization among multiple process then you would use a named mutex.
There is mainly on reason for wrapping a critical section in a class. This is to ensure that you leave the critical section when you're done using it.
// enter
EnterCriticalSection(&hCS);
// do critical stuff here...
// ...
//leave (you must remember this, or you'll most probably experience a dead lock)
LeaveCriticalSection(&hCS);
Wrapper classes usualy remember the last step (to leave) for you:
{
// enter critical section
CCriticalSection cs(...);
// do critical stuff
// ...
} // end of scope so the cs object is freed (destructor called and LeaveCriticalSection automagically called).
- petter
# 4 Re: Critical Section / Mutext questions.
I just declared CRITICAL_SECTION as a global in the class.
Then called InitializeCriticalSection( &CriticalSection ); in the class constructor.
Then I wrapped every function body in the class (because some are running one main thread and some on 2nd thread) with
EnterCriticalSection( &CriticalSection );
LeaveCriticalSection( &CriticalSection );
As far as i can tell it seems to have solved my crash...
# 5 Re: Critical Section / Mutext questions.
That will work fine in just about all cases of interest.
It won't work if an exception is thrown in the middle of one of your functions, since then you will never reach the call to LeaveCriticalSection(), and the CS will remain locked. Of course, since an exception was just thrown....
The "wrappers" you have seen are probably something like an MFC CSingleLock class, which are designed around the RAII ("resource acquisition is initialization") idiom. See this post for a good explanation and a good example: http://www.dev-archive.com/forum/showthread.php?t=387675&highlight=raii+critical+section
Mike