ThreadHandle: cleanup and docu.

This commit is contained in:
Robert Kloefkorn 2016-04-06 15:26:47 +02:00
parent 54ea243c5f
commit 7e6a6fb20a
2 changed files with 17 additions and 20 deletions

View File

@ -257,7 +257,7 @@ namespace Opm
namespace detail { namespace detail {
struct WriterCall : public ThreadHandle :: ObjectIF struct WriterCall : public ThreadHandle :: ObjectInterface
{ {
BlackoilOutputWriter& writer_; BlackoilOutputWriter& writer_;
std::unique_ptr< SimulatorTimerInterface > timer_; std::unique_ptr< SimulatorTimerInterface > timer_;

View File

@ -14,18 +14,18 @@ namespace Opm
class ThreadHandle class ThreadHandle
{ {
public: public:
class ObjectIF class ObjectInterface
{ {
protected: protected:
ObjectIF() {} ObjectInterface() {}
public: public:
virtual ~ObjectIF() {} virtual ~ObjectInterface() {}
virtual void run() = 0; virtual void run() = 0;
virtual bool isEndMarker () const { return false; } virtual bool isEndMarker () const { return false; }
}; };
protected: protected:
class EndObject : public ObjectIF class EndObject : public ObjectInterface
{ {
public: public:
void run () { } void run () { }
@ -37,21 +37,22 @@ namespace Opm
//////////////////////////////////////////// ////////////////////////////////////////////
class ThreadHandleObject class ThreadHandleObject
{ {
std::queue< ObjectIF* > objPtr_; protected:
std::queue< ObjectInterface* > objPtr_;
std::mutex mutex_; std::mutex mutex_;
// no copying // no copying
ThreadHandleObject( const ThreadHandleObject& ); ThreadHandleObject( const ThreadHandleObject& ) = delete;
public: public:
// constructor creating thread with given thread number // constructor creating object that is executed by thread
ThreadHandleObject() ThreadHandleObject()
: objPtr_(), mutex_() : objPtr_(), mutex_()
{ {
} }
//! insert object into queue //! insert object into threads queue
void push_back( ObjectIF* obj ) void push_back( ObjectInterface* obj )
{ {
// lock mutex to make sure objPtr is not used // lock mutex to make sure objPtr is not used
mutex_.lock(); mutex_.lock();
@ -59,18 +60,14 @@ namespace Opm
mutex_.unlock(); mutex_.unlock();
} }
//! return 1 of thread is stoped, 0 otherwise
int stoped() const
{
return ( objPtr_.empty() ) ? 1 : 0;
}
// do the work // do the work
void run() void run()
{ {
// wait until objects have been pushed to the queue
while( objPtr_.empty() ) while( objPtr_.empty() )
{ {
sleep( 1 ); // sleep one second
std::this_thread::sleep_for( std::chrono::seconds(1) );
} }
{ {
@ -78,7 +75,7 @@ namespace Opm
mutex_.lock(); mutex_.lock();
// get next object from queue // get next object from queue
std::unique_ptr< ObjectIF > obj( objPtr_.front() ); std::unique_ptr< ObjectInterface > obj( objPtr_.front() );
objPtr_.pop(); objPtr_.pop();
// unlock mutex for access to objPtr_ // unlock mutex for access to objPtr_
@ -112,7 +109,7 @@ namespace Opm
private: private:
// prohibit copying // prohibit copying
ThreadHandle( const ThreadHandle& ); ThreadHandle( const ThreadHandle& ) = delete;
public: public:
// default constructor // default constructor
@ -125,7 +122,7 @@ namespace Opm
} // end constructor } // end constructor
//! dispatch object to separate thread //! dispatch object to separate thread
void dispatch( ObjectIF* obj ) void dispatch( ObjectInterface* obj )
{ {
// add object to queue of objects // add object to queue of objects
threadObject_.push_back( obj ) ; threadObject_.push_back( obj ) ;