2012-06-14 09:06:06 -04:00
|
|
|
|
2012-09-23 17:23:33 -04:00
|
|
|
// IsThread.h
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Interfaces to the cIsThread class representing an OS-independent wrapper for a class that implements a thread.
|
|
|
|
|
|
|
|
/*
|
|
|
|
Usage:
|
2021-03-28 17:33:24 -04:00
|
|
|
To have a new thread, declare a class descending from cIsThread.
|
2012-06-14 09:06:06 -04:00
|
|
|
Then override its Execute() method to provide your thread processing.
|
|
|
|
In the descending class' constructor call the Start() method to start the thread once you're finished with initialization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cIsThread
|
|
|
|
{
|
|
|
|
public:
|
2021-03-28 17:33:24 -04:00
|
|
|
|
2021-03-28 08:34:57 -04:00
|
|
|
cIsThread(AString && a_ThreadName);
|
2014-03-09 13:52:12 -04:00
|
|
|
virtual ~cIsThread();
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2014-12-24 18:34:54 -05:00
|
|
|
/** Starts the thread; returns without waiting for the actual start. */
|
2021-03-28 17:33:24 -04:00
|
|
|
void Start(void);
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2014-12-24 18:34:54 -05:00
|
|
|
/** Signals the thread to terminate and waits until it's finished. */
|
2013-08-11 13:40:15 -04:00
|
|
|
void Stop(void);
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2014-06-08 15:58:08 -04:00
|
|
|
/** Returns true if the thread calling this function is the thread contained within this object. */
|
2014-10-18 19:29:34 -04:00
|
|
|
bool IsCurrentThread(void) const { return std::this_thread::get_id() == m_Thread.get_id(); }
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2021-03-28 17:33:24 -04:00
|
|
|
protected:
|
2014-06-08 15:58:08 -04:00
|
|
|
|
2021-03-28 17:33:24 -04:00
|
|
|
/** This function, overloaded by the descendants, is called in the new thread. */
|
|
|
|
virtual void Execute(void) = 0;
|
|
|
|
|
|
|
|
/** The overriden Execute() method should check this value periodically and terminate if this is true. */
|
|
|
|
std::atomic<bool> m_ShouldTerminate;
|
|
|
|
|
|
|
|
private:
|
2015-06-22 16:27:13 -04:00
|
|
|
|
|
|
|
/** The thread object which holds the created thread for later manipulation */
|
2014-10-18 19:29:34 -04:00
|
|
|
std::thread m_Thread;
|
2014-12-24 18:34:54 -05:00
|
|
|
|
2021-03-28 17:33:24 -04:00
|
|
|
/** The name of the thread, used to aid debugging in IDEs which support named threads */
|
|
|
|
AString m_ThreadName;
|
|
|
|
|
2014-12-24 18:34:54 -05:00
|
|
|
/** The event that is used to wait with the thread's execution until the thread object is fully initialized.
|
2015-06-22 16:27:13 -04:00
|
|
|
This prevents the IsCurrentThread() call to fail because of a race-condition where the thread starts before m_Thread has been fully assigned. */
|
2021-03-28 17:33:24 -04:00
|
|
|
cEvent m_Initialisation;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2021-03-28 17:33:24 -04:00
|
|
|
/** This is the main thread entrypoint.
|
|
|
|
Wrapper for Execute() that waits for the initialization event, to prevent race conditions in thread initialization. */
|
|
|
|
void Entrypoint(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2021-03-28 17:33:24 -04:00
|
|
|
/** Sets the name of the current thread to be the name provided in m_ThreadName. */
|
|
|
|
void SetThreadName() const;
|
|
|
|
} ;
|