Home > Software engineering >  CRITICAL_SECTION type of LockCount why are negative
CRITICAL_SECTION type of LockCount why are negative

Time:09-24

By definition as long as there is a thread should not be LockCount will only why I write will be greater than 0-2-6-7, this situation is that I write the wrong

 
DWORD WINAPI Thread (LPVOID lpParameter)
{

For (int I=NULL; i <10; I++)
{
Sleep (2000);
The EnterCriticalSection (& amp; Cs);
Printf (" a thread: % d % % x x \ n ", cs. LockCount, cs. RecursionCount, cs. OwningThread);
LeaveCriticalSection (& amp; Cs);
}

return NULL;

}
DWORD WINAPI Thread_1 (LPVOID lpParameter)
{

For (int I=NULL; i <10; I++)
{
Sleep (2000);
The EnterCriticalSection (& amp; Cs);
Printf (" number two threads: % d % % x x \ n ", cs. LockCount, cs. RecursionCount, cs. OwningThread);
LeaveCriticalSection (& amp; Cs);
}
return NULL;
}
DWORD WINAPI Thread_2 (LPVOID lpParameter)
{
//
For (int I=NULL; i <10; I++)
{

Sleep (2000);
The EnterCriticalSection (& amp; Cs);

Printf (" three threads: % d % % x x \ n ", cs. LockCount, cs. RecursionCount, cs. OwningThread);

LeaveCriticalSection (& amp; Cs);
}
return NULL;
}

Int _tmain (int arg c, _TCHAR * argv [])
{
//initialize
InitializeCriticalSection (& amp; Cs);
//InitializeCriticalSection (& amp; _cs);
Printf (" : x x x % % % \ n ", cs. LockCount, cs. RecursionCount, cs. OwningThread);
//start the thread

HANDLE haHWND=CreateThread (NULL, NULL, the Thread, NULL, NULL, NULL);
HANDLE haHWND_1=CreateThread (NULL, NULL, Thread_1, NULL, NULL, NULL);
HANDLE haHWND_2=CreateThread (NULL, NULL, Thread_2, NULL, NULL, NULL);

Sleep (1000 * 60 * 60);
return 0;
}

CodePudding user response:

Step through the see
 
CRITICAL_SECTION cs;
InitializeCriticalSection (& amp; Cs);
The EnterCriticalSection (& amp; Cs);
LeaveCriticalSection (& amp; Cs);


After the InitializeCriticalSection LockCount (1); RecursionCount (0); OwningThread (0)
After the EnterCriticalSection LockCount (0); RecursionCount (1); OwningThread (XXX);
After LeaveCriticalSection LockCount (1); RecursionCount (0); OwningThread (0)

LockCount negative is not locked, 0
a lock

CodePudding user response:

Can I lock, a deadlock. The value is 1 OwningThrea RecursionCount only three to four a ID only LockCount is almost erratic - 6-2
Refresh LockCount but after EnterCriticalSection structure is negative
After LeaveCriticalSection LockCount values to brushed - 1

CodePudding user response:

Use the compiler is VS2013 is that I write wrong code LockCount is what causes is negative or is negative

CodePudding user response:

CodePudding user response:

Explanation of's official website: https://msdn.microsoft.com/en-us/library/windows/hardware/ff541979 (v=versus 85). Aspx

CodePudding user response:

Your problem solved? I also encountered this kind of circumstance, the vs2013 lockcount enter the critical section is not - 2-6, no problem, I think code - 2 and 6 seems to be related to the system

CodePudding user response:

I see, the negative conversion into a 8-bit binary, you finally a 0 indicates that the critical region is lock, 1 is not lock, the second from bottom with 0 s and 1 s said if there is a thread to be awakened, and the rest of the several you pick up the income of a decimal value is waiting for the number of threads, for example - 22, binary is 11101010, the last one is 0 indicates the critical area unlocked, the penultimate 1 have a thread to be awakened, said he was "the last several covariates take 111010 to 101, said 5 thread is waiting for; For example, if you say that again - 6, the binary is 11111010, the last one is unlocked 0 indicates the critical area, the penultimate 1 have a thread to be awakened, said he was "the last several covariates take 1, 111110, said a thread is waiting for

CodePudding user response:

Check the MSDN is one of the Windows programmer must master the skills,
  • Related