// --------------------------------------------------------------------------- // Copyright (C) 1994 Borland International // thread.cpp // Demonstrates the container class libraries' implementation // of TThread and TCriticalSection. // It also uses the WINNT's signalling of events to handle // synchronization. // --------------------------------------------------------------------------- #include #include #include // prevent cout from being interrupted TCriticalSection CS; // prevent process thread from ending too soon const int NumThreads = 2; HANDLE Events[NumThreads]; // // class Thread // class Thread : public TThread { public: Thread( int id) : Id(id), Count(0) {} private: unsigned long Run(); int Id; int Count; }; unsigned long Thread::Run() { while (Count++ < 10) { Sleep(100); // let other thread have some time // don't let cout be interrupted TCriticalSection::Lock lock(CS); cout << "[Thread" << Id << "] Iteration " << Count << endl; } SetEvent(Events[Id]); // Tell main thread I'm done return 0; } int main() { try { int i; DWORD ErrCode; for (i=0; i