Threads/Threads.cpp

This sample program illustrates basic thread usage and control using several core synchronization classes and interfaces including:

Other concepts like exception handling and timestamping are shown using the following classes:

00001 //
00002 //  Copyright (c) 2006 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED,
00008 //  AS TO THE CORRECTNESS OF THIS CODE OR ANY DERIVATIVE
00009 //  WORKS WHICH INCORPORATE IT.
00010 //
00011 //  AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS
00012 //  AND EXPLICITLY DISCLAIMS ANY LIABILITY, INCLUDING
00013 //  CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00014 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00015 //
00016 //  Use, duplication, or disclosure by the U.S. Government is subject to
00017 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00018 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00019 //  Data and Computer Software), as applicable.
00020 //
00021 
00022 #include "stdafx.h"
00023 
00024 
00025 using namespace std;
00026 using namespace DWFCore;
00027 
00028 
00029 #ifdef  _DWFCORE_WIN32_SYSTEM   
00030 #define SLEEP( ms ) ( Sleep(ms) )
00031 #else
00032 #define SLEEP( ms ) ( usleep(ms) )
00033 #endif
00034 
00035 DWFCore::DWFThreadMutex g_oMutex;
00036 
00037 
00038 class worker : public DWFThreadWorker
00039 {
00040 public:
00041 
00042     int nSEQ;
00043 
00044 public:
00045 
00046     worker() throw() {;}
00047     virtual ~worker() throw() {;}
00048 
00049     void begin() throw()
00050     {
00051 
00052 #ifdef  _DWFCORE_WIN32_SYSTEM
00053         DWORD nTID = ::GetCurrentThreadId();
00054 #else
00055         pthread_t nTID = pthread_self();
00056 #endif
00057 
00058         g_oMutex.lock();
00059         cout << "Tick (" << DWFTimer::Tick64() << ")  START    ";
00060         cout << "Sequence [" << nSEQ << "] TID [" << nTID << "]" << "\n";
00061         cout.flush();
00062         g_oMutex.unlock();
00063 
00064         SLEEP( 150 );
00065 
00066         g_oMutex.lock();
00067         cout << "Tick (" << DWFTimer::Tick64() << ")  FINISH   ";
00068         cout << "Sequence [" << nSEQ << "] TID [" << nTID << "]" << "\n";
00069         cout.flush();
00070         g_oMutex.unlock();
00071 
00072     }
00073 };
00074 
00075 class lazyworker : public DWFThreadWorker
00076 {
00077 
00078 public:
00079 
00080     lazyworker() throw() {;}
00081     virtual ~lazyworker() throw() {;}
00082 
00083     void begin() throw()
00084     {
00085 
00086 #ifdef  _DWFCORE_WIN32_SYSTEM
00087         DWORD nTID = ::GetCurrentThreadId();
00088 #else
00089         pthread_t nTID = pthread_self();
00090 #endif
00091 
00092         g_oMutex.lock();
00093         cout << "Tick (" << DWFTimer::Tick64() << ")  START LAZYWORKER   ";
00094         cout << "TID [" << nTID << "]" << "\n";
00095         cout.flush();
00096         g_oMutex.unlock();
00097 
00098         SLEEP( 1000000 );
00099 
00100         g_oMutex.lock();
00101         cout << "LAZYWORKER Tick (" << DWFTimer::Tick64() << ")  FINISH   ";
00102         cout << "TID [" << nTID << "]" << "\n";
00103         cout.flush();
00104         g_oMutex.unlock();
00105 
00106     }
00107 };
00108 
00109 
00110 
00111 #define POOL    2
00112 #define THREADS 10
00113 
00114 int main()
00115 {
00116         g_oMutex.init();
00117 
00118         DWFCore::DWFThreadPool oPool;
00119         oPool.init( POOL );
00120 
00121         cout << "Thread pool initialized with " << POOL << " threads\n\n\n";
00122 
00123         worker w[THREADS];
00124 
00125         unsigned long i = 0;
00126         for (; i<THREADS; i++)
00127         {
00128             w[i].nSEQ = i;
00129             oPool.run( w[i] );
00130         }
00131 
00132         g_oMutex.lock();
00133         cout << "Tick (" << DWFTimer::Tick64() << ")  STARTED " << i << " workers\n\n";
00134         cout.flush();
00135         g_oMutex.unlock();
00136 
00137         lazyworker lz;
00138         DWFCore::DWFThreadPool::Controller* pLazyController = oPool.run( lz );
00139 
00140         g_oMutex.lock();
00141         cout << "Tick (" << DWFTimer::Tick64() << ")  waiting for lazy worker...\n";
00142         cout.flush();
00143         g_oMutex.unlock();
00144 
00145         for (i=0; i<THREADS; i++)
00146         {
00147             w[i].nSEQ = i;
00148             oPool.run( w[i] );
00149 
00150             if ((i == (THREADS/2)) && pLazyController)
00151             {
00152                 g_oMutex.lock();
00153                 cout << "Tick (" << DWFTimer::Tick64() << ")  ending lazy worker...\n";
00154                 cout.flush();
00155                 g_oMutex.unlock();
00156 
00157                 pLazyController->end();
00158 
00159                 g_oMutex.lock();
00160                 cout << "Tick (" << DWFTimer::Tick64() << ")  ended lazy worker...\n";
00161                 cout << "Tick (" << DWFTimer::Tick64() << ")  deleting lazy worker controller...\n";
00162                 cout.flush();
00163                 g_oMutex.unlock();
00164 
00165                 DWFCORE_FREE_OBJECT( pLazyController );
00166                 pLazyController = NULL;
00167 
00168                 g_oMutex.lock();
00169                 cout << "Tick (" << DWFTimer::Tick64() << ")  lazy worker controller deleted.\n";
00170                 cout.flush();
00171                 g_oMutex.unlock();
00172             }
00173         }
00174 
00175         g_oMutex.lock();
00176         cout << "Tick (" << DWFTimer::Tick64() << ")  STARTED " << i << " workers\n\n";
00177         cout.flush();
00178         g_oMutex.unlock();
00179 
00180         SLEEP( 2000 );
00181         
00182         g_oMutex.lock();
00183         cout << "Tick (" << DWFTimer::Tick64() << ")  END APP\n\n";
00184         cout.flush();
00185         g_oMutex.unlock();
00186 
00187 
00188         g_oMutex.lock();
00189         cout << "OK\n";
00190         cout.flush();
00191         g_oMutex.unlock();
00192 
00193         g_oMutex.destroy();
00194 
00195     return 0;
00196 }

Generated on Tue Jan 6 22:39:28 2009 for Autodesk DWF Core Library by  doxygen 1.4.5