libcyberradio  22.01.24
Thread.h
1 /***************************************************************************
2  * \file Thread.h
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 #ifndef INCLUDED_LIBCYBERRADIO_THREAD_H
12 #define INCLUDED_LIBCYBERRADIO_THREAD_H
13 
14 #include <boost/thread.hpp>
15 #include <boost/chrono.hpp>
16 #include <string>
17 
21 namespace LibCyberRadio
22 {
48  class Thread
49  {
50  public:
57  Thread(const std::string& name = "",
58  const std::string& cls = "");
64  Thread(const Thread& src);
70  virtual Thread& operator=(const Thread& src);
74  virtual ~Thread();
78  virtual void start();
85  virtual void run() = 0;
89  virtual void interrupt();
98  virtual void sleep(double secs);
103  virtual bool isRunning() const;
108  virtual void setName(const std::string& name);
113  virtual void setClass(const std::string& cls);
119  virtual boost::thread::id getId() const;
124  virtual std::string getIdString() const;
132  virtual void onInterrupt();
142  virtual void onException(const std::exception& ex);
143 
144  protected:
145  virtual void thisThreadRun();
146 
147  protected:
148  boost::thread _thisThread;
149  std::string _name;
150  std::string _class;
151  bool _isRunning;
152  };
153 
154 } /* namespace LibCyberRadio */
155 
156 #endif /* INCLUDED_LIBCYBERRADIO_THREAD_H */
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