Multithreading Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r3d dra6on
    New Member
    • Oct 2007
    • 5

    #1

    Multithreading Problem

    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 );
    }
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Hi,

    Can you post those errors here? You also need to write int main() instead of void main(). void is an old usage!! you also need to include iostream.h .

    Regards

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by zodilla58
      you also need to include iostream.h .
      You do not include iostream.h.

      That is the pre-ANSI C++ used before 1998.

      ANSI C++ header file names have no extension. ANSI C header file names are the ones with the .h extensions.

      Comment

      Working...