Locking/Unlocking functions with CRITICAL_SECTION

543 views Asked by At

So, when I use "EnterCriticalSection" & "LeaveCriticalSection" I throws an exception at me, this is my current setup:

void printer::Unlock()
{
    LeaveCriticalSection(&_cs);
}

void printer::Lock()
{
    EnterCriticalSection(&_cs);
}

_cs is a CRITICAL_SECTION object created inside my class "printer" like this:

class printer {
private:
    static CRITICAL_SECTION _cs;

When I call "Lock" it throws the exception, I'm not really sure why, I've tried reading the MSDN but I dont quite 100% understand it.

(I dont want to use mutexes...)

1

There are 1 answers

1
Curmudgeon On BEST ANSWER

I believe you need to add

InitializeCriticalSection(&_cs);

If that fails, you might try changing the CRITICAL_SECTION _cs to mutable rather than static, but that's kind of a shot in the dark.