60 class QFunctionThread :
public QThread {
65 QFunctionThread(
GThunk func);
75 int returnValue()
const;
84 Q_DISABLE_COPY(QFunctionThread)
134 virtual void join() = 0;
141 virtual bool join(
unsigned long ms) = 0;
148 virtual string
name()
const = 0;
173 virtual void sleep(
double ms) = 0;
178 virtual void start() = 0;
185 virtual void stop() = 0;
194 virtual void yield() = 0;
319 static void setGuiThread();
326 virtual void run() = 0;
363 class GThreadQt :
public GThread {
368 GThreadQt(
GThunk func,
const string& threadName =
"");
373 GThreadQt(
GThunkInt func,
const string& threadName =
"");
378 GThreadQt(QThread* qthread);
380 virtual ~GThreadQt()
override;
383 int getResult()
const override;
386 bool isRunning()
const override;
389 void join()
override;
392 bool join(
unsigned long ms)
override;
395 string name()
const override;
398 int priority()
const override;
401 void setName(
const string& name)
override;
404 void setPriority(
int priority)
override;
407 void sleep(
double ms)
override;
410 void start()
override;
413 void stop()
override;
416 void yield()
override;
423 Q_DISABLE_COPY(GThreadQt)
445 class GThreadStd :
public GThread {
450 GThreadStd(
GThunk func,
const string& threadName =
"");
455 GThreadStd(
GThunkInt func,
const string& threadName =
"");
460 GThreadStd(std::thread* stdThread);
462 virtual ~GThreadStd()
override;
465 int getResult()
const override;
468 bool isRunning()
const override;
471 void join()
override;
474 bool join(
unsigned long ms)
override;
477 string name()
const override;
480 int priority()
const override;
483 void setName(
const string& name)
override;
486 void setPriority(
int priority)
override;
489 void sleep(
double ms)
override;
492 void start()
override;
495 void stop()
override;
498 void yield()
override;
505 Q_DISABLE_COPY(GThreadStd)
507 std::thread* _stdThread;
509 std::atomic<bool> _running;
int _returnValue
Definition: gthread.h:332
friend class QtGui
Definition: gthread.h:343
static void runOnQtGuiThread(GThunk func)
Runs the given void function on the Qt GUI thread, blocking the current thread to wait until it is do...
Definition: gthread.cpp:152
std::function< int()> GThunkInt
An alias for a function wrapper around a function with no parameters and an int return (such as main(...
Definition: gtypes.h:36
virtual void start()=0
Tells the thread to start running.
static GThread * _qtGuiThread
Definition: gthread.h:335
GThread()
Definition: gthread.cpp:96
static bool wait(GThread *thread, long ms)
Waits the given number of milliseconds for the given thread to finish.
Definition: gthread.cpp:196
static GThread * getCurrentThread()
Returns the caller's Qt thread object.
Definition: gthread.cpp:107
virtual string name() const =0
Returns the thread's name as passed to the constructor, or a default name if none was passed...
GThunkInt _funcInt
Definition: gthread.h:330
static void ensureThatThisIsTheQtGuiThread(string message="")
Generates an error if the caller is not running on the Qt GUI thread.
Definition: gthread.cpp:100
static GThread * getQtGuiThread()
Returns the Qt thread object representing the Qt Gui thread for the application.
Definition: gthread.cpp:116
virtual int priority() const =0
Returns the thread's priority.
void native_set_thread_name(const char *name)
Definition: gthread.cpp:43
static GThread * _studentThread
Definition: gthread.h:336
virtual void setName(string name)=0
Sets the thread's name to the given value.
static void startStudentThread(GThunkInt mainFunc)
Starts the student's thread, telling it to run the given function, which accepts no arguments and ret...
Definition: gthread.cpp:185
virtual void stop()=0
Forcibly terminates the thread.
static bool iAmRunningOnTheQtGuiThread()
Returns true if the caller is running on the Qt GUI thread.
Definition: gthread.cpp:124
virtual void setPriority(int priority)=0
Sets the thread's priority to the given value.
static bool iAmRunningOnTheStudentThread()
Returns true if the caller is running on the student thread.
Definition: gthread.cpp:128
static void runInNewThread(GThunk func, string threadName="")
Runs the given void function in its own new thread, blocking the current thread to wait until it is d...
Definition: gthread.cpp:136
GThunk _func
Definition: gthread.h:329
The GThread class is a utility class containing static methods that allow you to run code on various ...
Definition: gthread.h:115
static std::map< QThread *, GThread * > _allGThreadsQt
Definition: gthread.h:339
static void runOnQtGuiThreadAsync(GThunk func)
Runs the given void function on the Qt GUI thread in the background; the current thread does not bloc...
Definition: gthread.cpp:165
bool _hasReturn
Definition: gthread.h:331
virtual int getResult() const =0
Returns the value returned by the thread's function.
virtual void yield()=0
Indicates that the current thread is willing to yield execution to any other threads that want to run...
Definition: gthread.cpp:215
static GThread * getStudentThread()
Returns the Qt thread object representing the thread on which the student's main() function runs...
Definition: gthread.cpp:120
virtual ~GThread()=default
std::function< void()> GThunk
An alias for a function wrapper around a void function with no parameters and no return.
Definition: gtypes.h:30
static bool studentThreadExists()
Returns true if the student's thread has already been created.
Definition: gthread.cpp:192
virtual bool isRunning() const =0
Returns true if the given thread is currently actively running.
static bool qtGuiThreadExists()
Returns true if the Qt GUI thread has been created.
Definition: gthread.cpp:132
static GThread * runInNewThreadAsync(GThunk func, string threadName="")
Runs the given void function in its own new thread in the background; the current thread does not blo...
Definition: gthread.cpp:146
static std::map< std::thread *, GThread * > _allGThreadsStd
Definition: gthread.h:340
virtual void join()=0
Waits for this thread to finish.
void native_thread_exit()
Definition: gthread.cpp:57
virtual void sleep(double ms)=0
Causes the thread to pause itself for the given number of milliseconds.