Hello,
i have often random crashes while entering the critical section.
InitializeCriticalSection(&m_lock); is saved as a class variable.
on
EnterCriticalSection(&m_lock);
it crashes randomly, especially when the function which locks/unlock get more often called.
LeaveCriticalSection(&m_lock);
on leaving
EnterCriticalSection(&m_lock);
LeaveCriticalSection(&m_lock);
DeleteCriticalSection(&m_lock);
on destroy class.
On every lock call it wont get locked a second time, and unlocks instantly when not used anymore.
Are there some things i have to look about, or some alternatives?
This programm have around 45 threads.
I think he's using the raw windows API as he's nominating the CriticalSections.
Let me tell you I think Windows applies some limitations to running threads in a single time for a single process.
1. Make sure you're not copying your CriticalSection class. It's your issue, I'm almost sure, make your copy constructor and assignment operator as private members and report back.
2. Make sure you're using _beginthreadex.
CRT hates CreateThread. _beginthreadex is the suggested way to go, and you can find it in process.h .
3. Use an alternative library, like Boost.
I'm developing my own library and multithreading and CriticalSection (hidden as Mutex) seems to be working very fine.
I'd guess it was a memory overwrite that's hitting the CS data structure. I've used critical sections with more threads than that in the server of an authentication service than ran for years.
Don't use a Mutex instead, just because you appear to have problems with CriticalSections.
You should be wrapping these Platform API objects in C++ or using a library (like Boost), but ensure that it's using CriticalSections and NOT Mutexes. The two are different things with different applications.
I know, but what I meant is, I call a CriticalSection "Mutex".
1 2 3
class Mutex {
CriticalSection cs;
};
That's what I meant.
Anyways, I'm still in the opinion he's copy-constructing it to another class, making it DeleteCriticalSection twice (Or at least making it access the CriticalSection after deletion).