home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / threads.pak / THREAD.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  2KB  |  91 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // thread.cpp
  4. //    Demonstrates the container class libraries' implementation
  5. //    of TThread and TCriticalSection.                          
  6. //    It also uses the WINNT's signalling of events to handle   
  7. //    synchronization.                                          
  8. // ---------------------------------------------------------------------------
  9.  
  10. #include <windows.h>
  11. #include <iostream.h>
  12. #include <classlib/thread.h>
  13.  
  14. // prevent cout from being interrupted
  15. TCriticalSection CS;                
  16.  
  17. // prevent process thread from ending too soon
  18. const int NumThreads = 2;
  19. HANDLE Events[NumThreads];
  20.  
  21. // 
  22. // class Thread
  23. // 
  24. class Thread : public TThread
  25. {
  26.   public:
  27.     Thread( int id) : Id(id), Count(0) {}
  28.  
  29.   private:
  30.     unsigned long Run();
  31.     int Id;
  32.     int Count;
  33. };
  34.  
  35. unsigned long Thread::Run()
  36. {
  37.     while (Count++ < 10)
  38.         {
  39.         Sleep(100);         // let other thread have some time
  40.         // don't let cout be interrupted
  41.         TCriticalSection::Lock lock(CS);
  42.         cout << "[Thread" << Id << "] Iteration " << Count << endl;
  43.         }
  44.     SetEvent(Events[Id]);   // Tell main thread I'm done
  45.     return 0;
  46. }
  47.  
  48. int main()
  49. {
  50.     try {
  51.         int i;
  52.         DWORD ErrCode;
  53.  
  54.         for (i=0; i<NumThreads; i++)
  55.             {
  56.             Events[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
  57.             if( Events[i] == NULL )
  58.                 throw(GetLastError());
  59.             }
  60.  
  61.         // create threads
  62.         Thread a(0);
  63.         Thread b(1);
  64.  
  65.         // start the threads
  66.         a.Start();
  67.         b.Start();
  68.  
  69.         // change priority of threads
  70.         a.SetPriority(THREAD_PRIORITY_NORMAL);
  71.         b.SetPriority(THREAD_PRIORITY_ABOVE_NORMAL);
  72.   
  73.         // threads have now started, wait until they're done.
  74.         ErrCode = WaitForMultipleObjects(NumThreads, Events, TRUE, INFINITE);
  75.         if( ErrCode == DWORD(-1) )
  76.             throw(GetLastError());
  77.   
  78.         // here if done.
  79.         for( i=0; i<NumThreads; i++ )
  80.             CloseHandle(Events[i]);
  81.  
  82.         cout << "Finished!" << endl;
  83.         }
  84.     catch (DWORD ErrCode)
  85.         {
  86.         // if any error
  87.         cout << "Errcode = " << ErrCode << endl;
  88.         }
  89.     return 0;
  90. }
  91.