I am not sure where I am going wrong with this. I get a lot of LINK errors, and I am unsure of how to solve the problem with them. Can someone please tell me where I went wrong?
Code:
#include <process.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
unsigned Counter;
int Px;
int Py;
int Qx;
int Qy;
HANDLE hMutex;
DWORD dwWaitResult;
unsigned _stdcall SecondThreadFunc ( void* pArguments )
{
bool hStayInLoop = true;
while ( hStayInLoop )
{
if ( Counter < 20 )
{
dwWaitResult = WaitForSingleObject (hMutex, 5000L);
switch (dwWaitResult)
{
case WAIT_OBJECT_0 :
{
int X;
int Y;
X = (Px + Qx)/2;
Y = (Py + Qy)/2;
printf ( "The mid-point is (", X, ",", Y, ")" );
Counter++;
hStayInLoop = false;
}
case WAIT_TIMEOUT :
hStayInLoop = false;
case WAIT_ABANDONED :
hStayInLoop = false;
}
}
}
_endthreadex ( 0 );
return 0;
}
void main ()
{
HANDLE hThread;
unsigned threadID;
hMutex = CreateMutex ( NULL, FALSE, "MtPD");
if (hMutex == NULL)
{
CloseHandle (hMutex);
cout << "Error creating Mutex." << endl;
}
hThread = (HANDLE) _beginthreadex ( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
bool bStayInLoop = true;
while ( bStayInLoop )
{
if ( Counter < 20 )
{
dwWaitResult = WaitForSingleObject (hMutex, 5000L);
switch (dwWaitResult)
{
case WAIT_OBJECT_0 :
{
__try
{
Px = (rand() % 10) + 1;
Py = (rand() % 10) + 1;
Qx = (rand() % 10) + 1;
Qy = (rand() % 10) + 1;
Counter++;
bStayInLoop = false;
ReleaseMutex (hMutex);
}
__finally
{
if (! ReleaseMutex(hMutex))
{
cout << "ERROR: Could not release Mutex.";
}
}
break;
}
case WAIT_TIMEOUT :
bStayInLoop = false;
case WAIT_ABANDONED :
bStayInLoop = false;
}
}
}
CloseHandle ( hThread );
CloseHandle ( hMutex );
}
Comment