56 class QFunctionThread :
public QThread {
61 QFunctionThread(GThunk func);
66 QFunctionThread(GThunkInt func);
71 int returnValue()
const;
80 Q_DISABLE_COPY(QFunctionThread)
130 virtual void join() = 0;
137 virtual bool join(
unsigned long ms) = 0;
144 virtual string
name()
const = 0;
169 virtual void sleep(
double ms) = 0;
174 virtual void start() = 0;
181 virtual void stop() = 0;
190 virtual void yield() = 0;
246 static void runInNewThread(GThunk func,
const string& threadName =
"");
315 static void setGuiThread();
322 virtual void run() = 0;
361 class GThreadQt :
public GThread {
366 GThreadQt(GThunk func,
const string& threadName =
"");
371 GThreadQt(GThunkInt func,
const string& threadName =
"");
376 GThreadQt(QThread* qthread);
378 virtual ~GThreadQt()
override;
387 void join()
override;
390 bool join(
unsigned long ms)
override;
393 string
name()
const override;
399 void setName(
const string& name)
override;
405 void sleep(
double ms)
override;
408 void start()
override;
411 void stop()
override;
414 void yield()
override;
421 Q_DISABLE_COPY(GThreadQt)
443 class GThreadStd :
public GThread {
448 GThreadStd(GThunk func,
const string& threadName =
"");
453 GThreadStd(GThunkInt func,
const string& threadName =
"");
458 GThreadStd(std::thread* stdThread);
460 virtual ~GThreadStd()
override;
469 void join()
override;
472 bool join(
unsigned long ms)
override;
475 string
name()
const override;
481 void setName(
const string& name)
override;
487 void sleep(
double ms)
override;
490 void start()
override;
493 void stop()
override;
496 void yield()
override;
503 Q_DISABLE_COPY(GThreadStd)
505 std::thread* _stdThread;
507 std::atomic<bool> _running;
512 void native_set_thread_name(
const char *name);
515 [[noreturn]]
void native_thread_exit();
virtual void sleep(double ms)=0
Causes the thread to pause itself for the given number of milliseconds.
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:132
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:181
virtual int getResult() const =0
Returns the value returned by the thread's function.
virtual void setPriority(int priority)=0
Sets the thread's priority to the given value.
virtual void join()=0
Waits for this thread to finish.
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:161
virtual ~GThread()=default
friend class QtGui
Definition: gthread.h:341
virtual int priority() const =0
Returns the thread's priority.
virtual void setName(string name)=0
Sets the thread's name to the given value.
static GThread * getQtGuiThread()
Returns the Qt thread object representing the Qt Gui thread for the application.
Definition: gthread.cpp:112
static GThread * getStudentThread()
Returns the Qt thread object representing the thread on which the student's main() function runs...
Definition: gthread.cpp:116
static void ensureThatThisIsTheQtGuiThread(string message="")
Generates an error if the caller is not running on the Qt GUI thread.
Definition: gthread.cpp:96
static GThread * _qtGuiThread
Definition: gthread.h:331
static GThread * getCurrentThread()
Returns the caller's Qt thread object.
Definition: gthread.cpp:103
GThunkInt _funcInt
Definition: gthread.h:326
static bool iAmRunningOnTheQtGuiThread()
Returns true if the caller is running on the Qt GUI thread.
Definition: gthread.cpp:120
virtual string name() const =0
Returns the thread's name as passed to the constructor, or a default name if none was passed...
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:211
static bool qtGuiThreadExists()
Returns true if the Qt GUI thread has been created.
Definition: gthread.cpp:128
static bool wait(GThread *thread, long ms)
Waits the given number of milliseconds for the given thread to finish.
Definition: gthread.cpp:192
int _returnValue
Definition: gthread.h:328
virtual void stop()=0
Forcibly terminates the thread.
GThread()
Definition: gthread.cpp:92
GThunk _func
Definition: gthread.h:325
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:142
bool _hasReturn
Definition: gthread.h:327
static GThread * _studentThread
Definition: gthread.h:332
static bool studentThreadExists()
Returns true if the student's thread has already been created.
Definition: gthread.cpp:188
virtual void start()=0
Tells the thread to start running.
static std::map< std::thread *, GThread * > _allGThreadsStd
Definition: gthread.h:336
virtual bool isRunning() const =0
Returns true if the given thread is currently actively running.
static bool iAmRunningOnTheStudentThread()
Returns true if the caller is running on the student thread.
Definition: gthread.cpp:124
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:148
static std::map< QThread *, GThread * > _allGThreadsQt
Definition: gthread.h:335
The GThread class is a utility class containing static methods that allow you to run code on various ...
Definition: gthread.h:111