libcyberradio  22.01.24
Thread.cpp
1 /***************************************************************************
2  * \file Thread.cpp
3  *
4  * \brief Basic threading using Boost Threads.
5  *
6  * \author DA
7  * \copyright Copyright (c) 2015-2021 CyberRadio Solutions, Inc.
8  *
9  */
10 
11 #include <boost/chrono.hpp>
12 #include <LibCyberRadio/Common/Thread.h>
13 #include <sstream>
14 #include <string>
15 
16 namespace LibCyberRadio
17 {
18 
19  Thread::Thread(const std::string& name, const std::string& cls)
20  {
21  _name = name;
22  _class = cls;
23  _isRunning = false;
24  _thisThread = boost::thread();
25  }
26 
27  Thread::Thread(const Thread& src)
28  {
29  _name = src._name;
30  _class = src._class;
31  _isRunning = src._isRunning;
32  }
33 
35  {
36  if ( this != &src )
37  {
38  _name = src._name;
39  _class = src._class;
40  _isRunning = src._isRunning;
41  }
42  return *this;
43  }
44 
46  {
47  if ( _thisThread.get_id() != boost::thread::id() )
48  {
49  _thisThread.interrupt();
50  _thisThread.join();
51  }
52  }
53 
55  {
56  _thisThread = boost::thread(&Thread::thisThreadRun, this);
57  pthread_setname_np(_thisThread.native_handle(), this->_name.c_str());
58  }
59 
61  {
62  _thisThread.interrupt();
63  }
64 
65  void Thread::sleep(double secs)
66  {
67  uint64_t u_ms = (uint64_t)secs * 1000;
68  boost::chrono::microseconds ms(u_ms);
69  boost::this_thread::sleep_for(boost::chrono::microseconds(ms));
70  }
71 
72  bool Thread::isRunning() const
73  {
74  return _isRunning;
75  }
76 
77  void Thread::setName(const std::string& name)
78  {
79  this->_name = name;
80  }
81 
82  void Thread::setClass(const std::string& cls)
83  {
84  _class = cls;
85  }
86 
88  {
89  }
90 
91  boost::thread::id Thread::getId() const
92  {
93  return _thisThread.get_id();
94  }
95 
96  std::string Thread::getIdString() const
97  {
98  std::string ret;
99  if ( _name != "" )
100  ret = _name;
101  else if ( _class != "" )
102  {
103  std::ostringstream oss;
104  oss << _class << "-" << _thisThread.get_id();
105  ret = oss.str();
106  }
107  return ret;
108  }
109 
110  void Thread::onException(const std::exception& ex)
111  {
112  }
113 
114  void Thread::thisThreadRun()
115  {
116  _isRunning = true;
117  try
118  {
119  run();
120  }
121  catch(boost::thread_interrupted& ex)
122  {
123  onInterrupt();
124  }
125  catch(std::exception& ex)
126  {
127  onException(ex);
128  }
129  _isRunning = false;
130  _thisThread = boost::thread();
131  }
132 
133 } /* namespace CyberRadio */
134 
virtual std::string getIdString() const
Gets the identifier string for this thread.
Definition: Thread.cpp:96
Thread(const std::string &name="", const std::string &cls="")
Creates a Thread object.
Definition: Thread.cpp:19
virtual bool isRunning() const
Determines if the thread is running or not.
Definition: Thread.cpp:72
virtual void run()=0
Executes the main processing loop for the thread.
virtual boost::thread::id getId() const
Gets the identifier of the underlying Boost thread.
Definition: Thread.cpp:91
virtual void interrupt()
Interrupts (stops) the thread.
Definition: Thread.cpp:60
virtual void start()
Starts thread processing.
Definition: Thread.cpp:54
virtual ~Thread()
Destroys a Thread object.
Definition: Thread.cpp:45
virtual void setClass(const std::string &cls)
Sets the class identifer string for the thread.
Definition: Thread.cpp:82
virtual Thread & operator=(const Thread &src)
Creates a Thread object by assignment from another Thread object.
Definition: Thread.cpp:34
Defines functionality for LibCyberRadio applications.
Definition: App.h:23
virtual void setName(const std::string &name)
Sets the name of the thread.
Definition: Thread.cpp:77
virtual void onException(const std::exception &ex)
Executes code that must run when an unhandled exception occurs within the thread. ...
Definition: Thread.cpp:110
Base class for a thread object, based on Boost Threads.
Definition: Thread.h:48
virtual void sleep(double secs)
Pauses thread execution for a given time, checking for user interrupts during that time...
Definition: Thread.cpp:65
virtual void onInterrupt()
Executes code that must run when the thread is interrupted.
Definition: Thread.cpp:87