Develop Biology
The language of life
bio::Threaded Class Reference

#include <Threaded.h>

+ Inheritance diagram for bio::Threaded:
+ Collaboration diagram for bio::Threaded:

Public Types

typedef pid_t ThreadId
 

Public Member Functions

 Threaded ()
 
virtual ~Threaded ()
 
virtual ThreadId GetThreadId ()
 
virtual bool IsRunning ()
 
virtual void Sleep (TimeUS us)
 
virtual bool Start ()
 
virtual bool Stop ()
 
virtual bool Work ()
 
- Public Member Functions inherited from bio::ThreadSafe
 ThreadSafe ()
 
 ThreadSafe (const ThreadSafe &toCopy)
 
virtual ~ThreadSafe ()
 
void LockThread () const
 
void UnlockThread () const
 

Static Public Member Functions

static ThreadId InvalidThreadId ()
 

Protected Member Functions

virtual void RequestStop ()
 

Static Protected Member Functions

static void * Worker (void *arg)
 

Protected Attributes

bool m_created
 
ThreadId m_id
 
bool m_running
 
bool m_stopRequested
 

Detailed Description

Threaded classes are a wrapper around whatever thread interface the system is using. This class moves whatever Work() you need to do into a new thread with a simple wrapper that is OS & c++ version independent.

NOTE: we're currently only supporting threading on c++11 and greater and on linux using c++98 on. We may or may not ever support c++98 threading on windows, etc. TODO: does pthread work on Apple and BSD?

NOTE: YOU MUST CALL STOP BEFORE DESTROYING *this!!!!

Definition at line 52 of file Threaded.h.

Member Typedef Documentation

◆ ThreadId

typedef pid_t bio::Threaded::ThreadId

Definition at line 57 of file Threaded.h.

Constructor & Destructor Documentation

◆ Threaded()

bio::Threaded::Threaded ( )

YOU MUST CALL STOP BEFORE DESTROYING *this!!!!

Definition at line 37 of file Threaded.cpp.

38 :
39//@formatter:off
40 #if BIO_CPP_VERSION < 11
41 #ifdef BIO_OS_IS_LINUX
42 //use m_thread default ctor
43 #endif
45 #else
46 m_thread(NULL),
47 #endif
48 //@formatter:on
49 m_created(false),
50 m_running(false),
51 m_stopRequested(false)
52{
53
54}
static ThreadId InvalidThreadId()
Definition: Threaded.h:65
bool m_created
Definition: Threaded.h:141
bool m_running
Definition: Threaded.h:142
ThreadId m_id
Definition: Threaded.h:130
bool m_stopRequested
Definition: Threaded.h:143

◆ ~Threaded()

bio::Threaded::~Threaded ( )
virtual

Definition at line 56 of file Threaded.cpp.

57{
59}
#define BIO_ASSERT(cond)
Definition: AssertMacros.h:29

References BIO_ASSERT, and m_running.

Member Function Documentation

◆ GetThreadId()

Threaded::ThreadId bio::Threaded::GetThreadId ( )
virtual
Returns
the id of our thread.

Definition at line 102 of file Threaded.cpp.

103{
104 //@formatter:off
105 #if BIO_CPP_VERSION < 11
106 return m_id;
107 #else
108 BIO_SANITIZE(m_thread,,return InvalidThreadId())
109 return m_thread->get_id();
110 #endif
111 //@formatter:on
112}
#define BIO_SANITIZE(test, success, failure)

References BIO_SANITIZE, InvalidThreadId(), and m_id.

◆ InvalidThreadId()

static ThreadId bio::Threaded::InvalidThreadId ( )
inlinestatic
Returns
what is hopefully not a proper ThreadId.

Definition at line 65 of file Threaded.h.

66 {
67 return 0;
68 }

Referenced by GetThreadId().

◆ IsRunning()

bool bio::Threaded::IsRunning ( )
virtual
Returns
whether or not Work() is being called by our thread.

Definition at line 61 of file Threaded.cpp.

62{
63 LockThread();
64 bool ret = m_running;
66 return ret;
67}
void LockThread() const
Definition: ThreadSafe.cpp:84
void UnlockThread() const
Definition: ThreadSafe.cpp:97

References bio::ThreadSafe::LockThread(), m_running, and bio::ThreadSafe::UnlockThread().

◆ RequestStop()

void bio::Threaded::RequestStop ( )
protectedvirtual

Sets m_stopRequested to true.

Definition at line 69 of file Threaded.cpp.

70{
71 LockThread();
72 m_stopRequested = true;
74}

References bio::ThreadSafe::LockThread(), m_stopRequested, and bio::ThreadSafe::UnlockThread().

Referenced by Stop().

◆ Sleep()

void bio::Threaded::Sleep ( TimeUS  us)
virtual

Release thread processing for us microseconds.

Parameters
us

Definition at line 170 of file Threaded.cpp.

171{
172 //@formatter:off
173 #if BIO_CPP_VERSION < 11
174 #ifdef BIO_OS_IS_LINUX
175 usleep(us);
176 #endif
177 #else
178 std::this_thread::sleep_for (std::chrono::microseconds(ms));
179 #endif
180 //@formatter:on
181}

◆ Start()

bool bio::Threaded::Start ( )
virtual

Starts our thread, which will continuously call Work() until Stop()ed (or Work() exits).

Returns
whether or not our thread was successfully created.

Definition at line 114 of file Threaded.cpp.

115{
116 LockThread();
117 bool isStopped = !m_created && !m_running;
118 UnlockThread();
119 //@formatter:off
120 BIO_SANITIZE(!isStopped, ,return true)
121 //@formatter:on
122
123 //@formatter:off
124 #if BIO_CPP_VERSION < 11
125 BIO_SANITIZE(m_running,,return false)
126 #ifdef BIO_OS_IS_LINUX
127 int result = pthread_create(&m_thread, NULL, Worker, this);
128 #endif
129 m_created = result == 0;
130 #else
131 BIO_SANITIZE(m_thread,,return false)
132 m_thread = new std::thread(&Threaded::Worker, this);
133 m_created = true
134 #endif
135 //@formatter:on
136 return m_created;
137}
static void * Worker(void *arg)
Definition: Threaded.cpp:76

References BIO_SANITIZE, bio::ThreadSafe::LockThread(), m_created, m_running, bio::ThreadSafe::UnlockThread(), and Worker().

◆ Stop()

bool bio::Threaded::Stop ( )
virtual

Instructs our thread to stop calling Work() and joins our thread.

Returns
whether or not our thread was successfully joined.

Definition at line 139 of file Threaded.cpp.

140{
141 LockThread();
142 bool isStopped = !m_created && !m_running;
143 UnlockThread();
144
145 BIO_SANITIZE(isStopped, ,
146 return true)
147 //@formatter:off
148 #if BIO_CPP_VERSION < 11
149 m_stopRequested = true;
150 #ifdef BIO_OS_IS_LINUX
151 void** threadReturn;
152 int result = pthread_join(m_thread, threadReturn);
153 #endif
154 RequestStop();
155 m_created = false;
156 return result == 0;
157 #else
158 BIO_SANITIZE(m_thread,,return true)
159 RequestStop();
160 m_thread->join();
161 delete m_thread;
162 m_thread = NULL;
163 m_created = false;
164 return true;
165 #endif
166 //@formatter:on
167
168}
virtual void RequestStop()
Definition: Threaded.cpp:69

References BIO_SANITIZE, bio::ThreadSafe::LockThread(), m_created, m_running, m_stopRequested, RequestStop(), and bio::ThreadSafe::UnlockThread().

◆ Work()

virtual bool bio::Threaded::Work ( )
inlinevirtual

Does the actual work. Will be called repeatedly until either: Stop() is called OR this method returns false. So, just return false when you want to stop being a thread. You may want to Sleep() (below) after you Work() ;)

Returns
false when you want to stop; true if you want to run until Stop()ed.

Reimplemented in bio::physical::ThreadedPeriodic.

Definition at line 89 of file Threaded.h.

90 {
91 //your code goes here!
92 return false;
93 }

Referenced by Worker().

◆ Worker()

void * bio::Threaded::Worker ( void *  arg)
staticprotected
Parameters
arga Threaded*
Returns
NULL

Definition at line 76 of file Threaded.cpp.

77{
78 Threaded* threaded = Cast< Threaded* >(arg);
79 BIO_SANITIZE(threaded, ,
80 return NULL);
81
82 threaded->LockThread();
83 threaded->m_running = true;
84 threaded->UnlockThread();
85
86 bool again = true;
87 while (again)
88 {
89 bool again = threaded->Work();
90 threaded->LockThread();
91 again = again && threaded->m_stopRequested;
92 threaded->UnlockThread();
93 }
94
95 threaded->LockThread();
96 threaded->m_running = false;
97 threaded->UnlockThread();
98
99 return NULL;
100}

References BIO_SANITIZE, bio::ThreadSafe::LockThread(), m_running, m_stopRequested, bio::ThreadSafe::UnlockThread(), and Work().

Referenced by Start().

Member Data Documentation

◆ m_created

bool bio::Threaded::m_created
protected

Definition at line 141 of file Threaded.h.

Referenced by Start(), and Stop().

◆ m_id

ThreadId bio::Threaded::m_id
protected

Definition at line 130 of file Threaded.h.

Referenced by GetThreadId().

◆ m_running

bool bio::Threaded::m_running
protected

Definition at line 142 of file Threaded.h.

Referenced by ~Threaded(), IsRunning(), Start(), Stop(), and Worker().

◆ m_stopRequested

bool bio::Threaded::m_stopRequested
protected

Definition at line 143 of file Threaded.h.

Referenced by RequestStop(), Stop(), and Worker().


The documentation for this class was generated from the following files: