Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

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:

#include "stdafx.h"


using namespace std;
using namespace DWFCore;


#ifdef  _DWFCORE_WIN32_SYSTEM   
#define SLEEP( ms ) ( Sleep(ms) )
#else
#define SLEEP( ms ) ( usleep(ms) )
#endif

DWFCore::DWFThreadMutex g_oMutex;


class worker : public DWFThreadWorker
{
public:

    int nSEQ;

public:

    worker() throw() {;}
    virtual ~worker() throw() {;}

    void begin() throw()
    {

#ifdef  _DWFCORE_WIN32_SYSTEM
        DWORD nTID = ::GetCurrentThreadId();
#else
        pthread_t nTID = pthread_self();
#endif

        g_oMutex.lock();
        cout << "Tick (" << DWFTimer::Tick64() << ")  START    ";
        cout << "Sequence [" << nSEQ << "] TID [" << nTID << "]" << "\n";
        cout.flush();
        g_oMutex.unlock();

        SLEEP( 150 );

        g_oMutex.lock();
        cout << "Tick (" << DWFTimer::Tick64() << ")  FINISH   ";
        cout << "Sequence [" << nSEQ << "] TID [" << nTID << "]" << "\n";
        cout.flush();
        g_oMutex.unlock();

    }
};

class lazyworker : public DWFThreadWorker
{

public:

    lazyworker() throw() {;}
    virtual ~lazyworker() throw() {;}

    void begin() throw()
    {

#ifdef  _DWFCORE_WIN32_SYSTEM
        DWORD nTID = ::GetCurrentThreadId();
#else
        pthread_t nTID = pthread_self();
#endif

        g_oMutex.lock();
        cout << "Tick (" << DWFTimer::Tick64() << ")  START LAZYWORKER   ";
        cout << "TID [" << nTID << "]" << "\n";
        cout.flush();
        g_oMutex.unlock();

        SLEEP( 1000000 );

        g_oMutex.lock();
        cout << "LAZYWORKER Tick (" << DWFTimer::Tick64() << ")  FINISH   ";
        cout << "TID [" << nTID << "]" << "\n";
        cout.flush();
        g_oMutex.unlock();

    }
};



#define POOL    2
#define THREADS 10

int main()
{
    g_oMutex.init();

    try
    {
        DWFCore::DWFThreadPool oPool;
        oPool.init( POOL );

        cout << "Thread pool initialized with " << POOL << " threads\n\n\n";

        worker w[THREADS];

        unsigned long i = 0;
        for (; i<THREADS; i++)
        {
            w[i].nSEQ = i;
            oPool.run( w[i] );
        }

        g_oMutex.lock();
        cout << "Tick (" << DWFTimer::Tick64() << ")  STARTED " << i << " workers\n\n";
        cout.flush();
        g_oMutex.unlock();

        lazyworker lz;
        DWFCore::DWFThreadPool::Controller* pLazyController = oPool.run( lz );

        g_oMutex.lock();
        cout << "Tick (" << DWFTimer::Tick64() << ")  waiting for lazy worker...\n";
        cout.flush();
        g_oMutex.unlock();

        for (i=0; i<THREADS; i++)
        {
            w[i].nSEQ = i;
            oPool.run( w[i] );

            if ((i == (THREADS/2)) && pLazyController)
            {
                g_oMutex.lock();
                cout << "Tick (" << DWFTimer::Tick64() << ")  ending lazy worker...\n";
                cout.flush();
                g_oMutex.unlock();

                pLazyController->end();

                g_oMutex.lock();
                cout << "Tick (" << DWFTimer::Tick64() << ")  ended lazy worker...\n";
                cout << "Tick (" << DWFTimer::Tick64() << ")  deleting lazy worker controller...\n";
                cout.flush();
                g_oMutex.unlock();

                DWFCORE_FREE_OBJECT( pLazyController );
                pLazyController = NULL;

                g_oMutex.lock();
                cout << "Tick (" << DWFTimer::Tick64() << ")  lazy worker controller deleted.\n";
                cout.flush();
                g_oMutex.unlock();
            }
        }

        g_oMutex.lock();
        cout << "Tick (" << DWFTimer::Tick64() << ")  STARTED " << i << " workers\n\n";
        cout.flush();
        g_oMutex.unlock();

        SLEEP( 2000 );
        
        g_oMutex.lock();
        cout << "Tick (" << DWFTimer::Tick64() << ")  END APP\n\n";
        cout.flush();
        g_oMutex.unlock();


        g_oMutex.lock();
        cout << "OK\n";
        cout.flush();
        g_oMutex.unlock();
    }
    catch (DWFCore::DWFException& ex)
    {
        g_oMutex.lock();
        wcout << ex.type() << endl;
        wcout << ex.message() << endl;
        wcout << ex.function() << endl;
        wcout << ex.file() << endl;
        wcout << ex.line() << endl;
        g_oMutex.unlock();
    }

    g_oMutex.destroy();

    return 0;
}

Generated on Tue May 17 12:05:10 2005 for Autodesk DWF Core Library by  doxygen 1.4.1