Changeset 425 for experimental


Ignore:
Timestamp:
Oct 21, 2012, 12:05:47 AM (11 years ago)
Author:
Torben Dannhauer
Message:
 
Location:
experimental/Threading/Threading
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • experimental/Threading/Threading/SimHost.cpp

    r421 r425  
    3333        OpenThreads::Barrier syncBarrier;
    3434
    35         // Create ChannelWorkers
    36         OSG_ALWAYS<<"Creating ChannelWorkers..."<<std::endl;
     35        // Create IG Connectors
     36        OSG_ALWAYS<<"Creating IG Connectors..."<<std::endl;
    3737        for(int i=0; i<numThreads-1; ++i)
    3838        {
    39                 osg::ref_ptr<ChannelWorker> worker = new ChannelWorker();
    40                 _channelWorker.push_back(worker);
     39                osg::ref_ptr<ThreadedWorker> threadObject = new ThreadedWorker();
     40                osg::ref_ptr<IGConnector> IGCon = new IGConnector();
     41                threadObject->setThreadWorker(IGCon);
     42                _threadObjects.push_back(threadObject);
     43                _IGConnectors.push_back(IGCon);
    4144        }
    4245        OSG_ALWAYS<<"...done."<<std::endl;
     
    5558{
    5659        // Launch threads
    57     for(int i=0;i<static_cast<int>(_channelWorker.size());++i)
     60    for(int i=0;i<static_cast<int>(_threadObjects.size());++i)
    5861        {
    5962                int status;
    6063               
    6164                //thread->setStackSize(1024*256);
    62                 status = _channelWorker[i]->start();
     65                status = _threadObjects[i]->start();
    6366                assert(status == 0);
    6467        }
    6568
    6669        // Do something in the main thread
    67         for(int i=0;i<10000000;i++)
    68                 OSG_ALWAYS<<"";
     70                for(int i=0;i<10000000;i++)     OSG_ALWAYS<<"";
    6971       
    7072        // Signal threads to finish
    71     for(int i=0;i<static_cast<int>(_channelWorker.size());++i)
     73    for(int i=0;i<static_cast<int>(_threadObjects.size());++i)
    7274        {
    73                 //_channelWorker[i]->setDone(true);     // non blocking
    74                 _channelWorker[i]->cancel();    // Blocking
     75                //_threadObjects[i]->setDone(true);     // non blocking
     76                _threadObjects[i]->cancel();    // Blocking
    7577        }
    7678
     
    7880                OSG_ALWAYS<<"";
    7981
    80         //syncBarrier.block(numThreads);  // Block until all threads are ready
    8182}
  • experimental/Threading/Threading/SimHost.h

    r420 r425  
    55#include <osg/ref_ptr>
    66
    7 #include "ChannelWorker.h"
     7#include "ThreadedWorker.h"
     8#include "IGConnector.h"
    89
    910
     
    1819
    1920private:
    20         std::vector<osg::ref_ptr<ChannelWorker> > _channelWorker;
     21        std::vector<osg::ref_ptr<IGConnector> > _IGConnectors;
     22        std::vector<osg::ref_ptr<ThreadedWorker> > _threadObjects;
    2123
    2224};
  • experimental/Threading/Threading/ThreadWorkerBase.h

    r422 r425  
    11#pragma once
     2#include <osg/Referenced>
     3
     4
     5class ThreadedWorker;
    26
    37/**
     
    59 *
    610 * Just overload the operator() with your code you want to execute threaded in a loop.
    7  * Each execution will be completed by the thread, if the thread is cancels during a run, the cancelation will be postponed until the execution of the functor is completed.
     11 * Each execution will be completed by the thread, if the thread is set as done/canceled during a run, the cancelation will be postponed until the execution of the functor is completed.
    812 *
    9  * Note: This functor will be executed in a loop until the thread is canceled or the parameter done is set to true.
     13 * Note: This functor will be executed in a loop until the thread is canceled or set as done=true.
    1014 *
    1115 * @author Torben Dannhauer
    1216 * @date  Okt 2012
    1317 */
    14 class ThreadWorkerBase
     18class ThreadWorkerBase : public osg::Referenced
    1519{
    1620public:
     
    1822        virtual ~ThreadWorkerBase() ;
    1923
    20         void operator() (bool& _threadDone);
     24        virtual void operator() (ThreadedWorker* threadObject) = 0;
     25
     26
     27
    2128};
  • experimental/Threading/Threading/ThreadedWorker.cpp

    r424 r425  
    11#include "ThreadedWorker.h"
    22#include <osg/Notify>
     3#include <OpenThreads/ScopedLock>
    34
    45
     
    1920
    2021        // Do the work
    21         int i=0;
    2222        do
    2323        {
    24                 OSG_ALWAYS<<"ID "<<getThreadId()<<" :: itr "<<i++<<std::endl;
     24                OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadWorkerMutex);
    2525
    26                 // Place Holder
    27                 for(int i=0;i<100;i++)
    28                 OSG_ALWAYS<<"";
    29 
    30         } while (!testCancel() && !_done);      // repeat as long it is canceld or set as Done
     26                if( _threadWorker.valid() )
     27                        (*_threadWorker)(this);
     28               
     29        } while (_threadWorker!=NULL && !testCancel() && !_done);       // repeat as long as a functor is set and it is not canceled or set as Done
    3130}
    3231
     
    6059    return 0;
    6160}
     61
     62void ThreadedWorker::setThreadWorker( ThreadWorkerBase* tworker)
     63{
     64        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadWorkerMutex);
     65
     66        _threadWorker = tworker;
     67}
  • experimental/Threading/Threading/ThreadedWorker.h

    r424 r425  
    22
    33#include <osg/Referenced>
     4#include <osg/ref_ptr>
    45#include <OpenThreads/Thread>
     6#include <OpenThreads/Mutex>
     7
     8#include "ThreadWorkerBase.h"
     9
    510
    611/**
    712 * \brief This class is a Thread Object which allows to create Functors and attach them to this thread object.
     13 *
     14 * Subclass ThreadWorkerBase to implement your own functor to execute your custom code threaded.
     15 *
     16 * Use setDone(true) to shut down the thread nonblocking, or call cancel() to shut down the thread blocking (both called from outside of the workerthread).
     17 * Inside the workerthread's functor, you can set the functor parameter done=true to shutdown the thread after the current execution.
     18 *
    819 *
    920 * @author Torben Dannhauer
     
    1829        virtual int cancel();
    1930       
    20         void setDone(bool done);        // sinbgals the thread to stop but returns immediately. Use cancel if you want to wait blocking until the thread is canceled.
     31        void setDone(bool done);        // signals the thread to stop but returns immediately. Use cancel if you want to wait blocking until the thread is canceled.
    2132        bool getDone() const { return _done; } 
     33
     34        void setThreadWorker( ThreadWorkerBase* tworker);       // This function sets the functor which should be executed threaded. Set NULL to remove the current functor and cancel the thread.
     35        ThreadWorkerBase* getCurrentThreadWorker()              {return(_threadWorker);};
    2236
    2337private:
    2438        bool _done;
     39        osg::ref_ptr<ThreadWorkerBase> _threadWorker;
     40        OpenThreads::Mutex _threadWorkerMutex;  // Ensures the functor is not replacable while it is executed
    2541};
  • experimental/Threading/Threading/Threading.vcproj

    r424 r425  
    182182                        >
    183183                        <File
     184                                RelativePath=".\IGConnector.cpp"
     185                                >
     186                        </File>
     187                        <File
    184188                                RelativePath=".\main.cpp"
    185189                                >
     
    204208                        >
    205209                        <File
     210                                RelativePath=".\IGConnector.h"
     211                                >
     212                        </File>
     213                        <File
    206214                                RelativePath=".\SimHost.h"
    207215                                >
  • experimental/Threading/Threading/main.cpp

    r420 r425  
    99int main( int argc, char **argv )
    1010{
    11 
    1211        // Create osgVisual::SimHost
    13         OSG_ALWAYS<<"Creating SimHost..."<<std::endl;
    1412        osg::ref_ptr<SimHost> simulationHost = new SimHost();
    15         OSG_ALWAYS<<"...done."<<std::endl;
    1613
    1714        //Start it
Note: See TracChangeset for help on using the changeset viewer.