The GThread class is a utility class containing static methods that allow you to run code on various system threads. More...
#include "gthread.h"
Public Member Functions | |
int | getResult() const =0 |
Returns the value returned by the thread's function. More... | |
bool | isRunning() const =0 |
Returns true if the given thread is currently actively running. More... | |
void | join()=0 |
Waits for this thread to finish. More... | |
bool | join(unsigned long ms)=0 |
Waits for this thread to finish. More... | |
string | name() const =0 |
Returns the thread's name as passed to the constructor, or a default name if none was passed. More... | |
int | priority() const =0 |
Returns the thread's priority. More... | |
void | setName(string name)=0 |
Sets the thread's name to the given value. More... | |
void | setPriority(int priority)=0 |
Sets the thread's priority to the given value. More... | |
void | sleep(double ms)=0 |
Causes the thread to pause itself for the given number of milliseconds. More... | |
void | start()=0 |
Tells the thread to start running. More... | |
void | stop()=0 |
Forcibly terminates the thread. More... | |
void | yield()=0 |
Indicates that the current thread is willing to yield execution to any other threads that want to run. More... | |
Static Public Member Functions | |
static void | ensureThatThisIsTheQtGuiThread(string message="") |
Generates an error if the caller is not running on the Qt GUI thread. More... | |
static GThread * | getCurrentThread() |
Returns the caller's Qt thread object. More... | |
static GThread * | getQtGuiThread() |
Returns the Qt thread object representing the Qt Gui thread for the application. More... | |
static GThread * | getStudentThread() |
Returns the Qt thread object representing the thread on which the student's main() function runs. More... | |
static bool | iAmRunningOnTheQtGuiThread() |
Returns true if the caller is running on the Qt GUI thread. More... | |
static bool | iAmRunningOnTheStudentThread() |
Returns true if the caller is running on the student thread. More... | |
static bool | qtGuiThreadExists() |
Returns true if the Qt GUI thread has been created. More... | |
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 done. More... | |
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 block and keeps going. More... | |
static void | runOnQtGuiThread(GThunk func) |
Runs the given void function on the Qt GUI thread, blocking the current thread to wait until it is done. More... | |
static void | runOnQtGuiThreadAsync(GThunk func) |
Runs the given void function on the Qt GUI thread in the background; the current thread does not block and keeps going. More... | |
static void | startStudentThread(GThunkInt mainFunc) |
Starts the student's thread, telling it to run the given function, which accepts no arguments and returns an int. More... | |
static bool | studentThreadExists() |
Returns true if the student's thread has already been created. More... | |
static bool | wait(GThread *thread, long ms) |
Waits the given number of milliseconds for the given thread to finish. More... | |
Protected Member Functions | |
GThread() | |
~GThread()=default | |
void | run()=0 |
Protected Attributes | |
GThunk | _func |
GThunkInt | _funcInt |
bool | _hasReturn |
int | _returnValue |
Static Protected Attributes | |
static std::map< QThread *, GThread * > | _allGThreadsQt |
static std::map< std::thread *, GThread * > | _allGThreadsStd |
static GThread * | _qtGuiThread = nullptr |
static GThread * | _studentThread = nullptr |
The GThread class is a utility class containing static methods that allow you to run code on various system threads.
The library has the following two standard threads running at all times:
The Qt GUI thread, which runs Qt's master exec() loop, handles all GUI object creation and events (this is technically the program's main thread)
Students and clients normally do not need to worry about threading issues. These methods are called internally by many of the graphical interactors to make sure that all internal Qt GUI widgets are initialized on the Qt GUI thread. This is required for them to function properly.
If you want to run a piece of code in its own thread, use static methods GThread::runInNewThread
and GThread::runInNewThreadAsync
.
|
protected |
|
protectedvirtualdefault |
|
static |
Generates an error if the caller is not running on the Qt GUI thread.
An optional error detail message can be passed.
|
static |
Returns the caller's Qt thread object.
|
static |
Returns the Qt thread object representing the Qt Gui thread for the application.
|
pure virtual |
Returns the value returned by the thread's function.
This will be 0 until the function is done running. This method only has meaning if your thread executes a function that returns an int.
|
static |
Returns the Qt thread object representing the thread on which the student's main() function runs.
|
static |
Returns true if the caller is running on the Qt GUI thread.
|
static |
Returns true if the caller is running on the student thread.
|
pure virtual |
Returns true if the given thread is currently actively running.
|
pure virtual |
Waits for this thread to finish.
Will wait indefinitely as needed.
|
pure virtual |
Waits for this thread to finish.
Will wait up to the given number of milliseconds. Returns true if the thread has finished or false if it is still running.
|
pure virtual |
Returns the thread's name as passed to the constructor, or a default name if none was passed.
Not all thread implementations support names.
|
pure virtual |
Returns the thread's priority.
Threads with higher priorities tend to run more than ones that are lower. Not all thread implementations support priorities.
|
static |
Returns true if the Qt GUI thread has been created.
This will happen right before the student's main() function runs.
|
protectedpure virtual |
|
static |
Runs the given void function in its own new thread, blocking the current thread to wait until it is done.
You can pass an optional name for the thread which can help when looking through the list of threads in a debugger.
Any uncaught exceptions or errors in the new thread will crash the program and cannot be caught by the calling thread.
If you want the new thread to run in the background, use the runInNewThreadAsync
function instead.
Runs the given void function in its own new thread in the background; the current thread does not block and keeps going.
You can pass an optional name for the thread which can help when looking through the list of threads in a debugger. Returns a pointer to the given thread in case you want to wait a given amount of time for the thread to do its work.
Any uncaught exceptions or errors in the new thread will crash the program and cannot be caught by the calling thread.
If you want the caller to wait for the new thread to finish running, use the runInNewThread
function instead.
|
static |
Runs the given void function on the Qt GUI thread, blocking the current thread to wait until it is done.
This function is called heavily by the internal GUI widgets and interactors of the library, because all Qt GUI operations are required to be done on the application's main thread.
Any uncaught exceptions or errors in the Qt GUI thread will crash the program and cannot be caught by the calling thread.
If you want the new thread to run in the background, use the runOnQtGuiThreadAsync
function instead.
|
static |
Runs the given void function on the Qt GUI thread in the background; the current thread does not block and keeps going.
Any uncaught exceptions or errors in the Qt GUI thread will crash the program and cannot be caught by the calling thread.
If you want the caller to wait for the Qt GUI thread code to finish running, use the runOnQtGuiThread
function instead.
|
pure virtual |
Sets the thread's name to the given value.
Not all thread implementations support names.
|
pure virtual |
Sets the thread's priority to the given value.
Not all thread implementations support priorities.
|
pure virtual |
Causes the thread to pause itself for the given number of milliseconds.
ErrorException | if ms is negative |
|
pure virtual |
Tells the thread to start running.
|
static |
Starts the student's thread, telling it to run the given function, which accepts no arguments and returns an int.
|
pure virtual |
Forcibly terminates the thread.
You probably should not call this unless absolutely necessary, since it can lead to messed up state in the program.
|
static |
Returns true if the student's thread has already been created.
|
static |
Waits the given number of milliseconds for the given thread to finish.
|
pure virtual |
Indicates that the current thread is willing to yield execution to any other threads that want to run.
This differs slightly from sleep() in that sleep() mandates to pause the current thread for a given amount of time, while yield() is more of an offer to other threads that they may run now if they so choose.
|
friend |
|
staticprotected |
|
staticprotected |
|
protected |
|
protected |
|
protected |
|
staticprotected |
|
protected |
|
staticprotected |