reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofThread.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofConstants.h"
4#ifndef TARGET_NO_THREADS
5
6#include <atomic>
7#include <thread>
8#include <mutex>
9#include <condition_variable>
10
11
70class ofThread {
71public:
73 ofThread();
74
77 bool isThreadRunning() const;
78
81 std::thread::id getThreadId() const;
82
85 std::string getThreadName() const;
86
87 void setThreadName(const std::string & name);
88
92 void startThread();
93
99 OF_DEPRECATED_MSG("Use tryLock instead of setting the type of lock on startThread",
100 void startThread(bool mutexBlocks));
101
110 bool lock();
111
120 bool tryLock();
121
126 void unlock();
127
135 void stopThread();
136
170 void waitForThread(bool callStopThread = true,
171 long milliseconds = INFINITE_JOIN_TIMEOUT);
172
207 void sleep(long milliseconds);
208
218 void yield();
219
250 bool isCurrentThread() const;
251
259 std::thread& getNativeThread();
260
268 const std::thread & getNativeThread() const;
269
270
271 enum {
276 };
277
278protected:
313 virtual void threadedFunction();
314
316 std::thread thread;
317
325 mutable std::mutex mutex;
326
327private:
329 void run();
330
332 std::atomic<bool> threadRunning;
333 std::atomic<bool> threadDone;
334
336 std::atomic<bool> mutexBlocks;
337
338 std::string name;
339 std::condition_variable condition;
340
341
342};
343
344#else
345
346class ofThread{
347public:
348 void lock(){}
349 void unlock(){}
350 void startThread(){}
351 void stopThread(){};
352 bool isThreadRunning(){return false;}
353
354 enum {
355 INFINITE_JOIN_TIMEOUT = LONG_MAX
356 };
357};
358#endif
A threaded base class with a built in mutex for convenience.
Definition ofThread.h:70
void stopThread()
Stop the thread.
Definition ofThread.cpp:90
void startThread()
Start the thread.
Definition ofThread.cpp:38
void yield()
Tell the thread to give up its CPU time other threads.
Definition ofThread.cpp:128
std::string getThreadName() const
Get the unique thread name, in the form of "Thread id#".
Definition ofThread.cpp:28
std::mutex mutex
The internal mutex called through lock() & unlock().
Definition ofThread.h:325
bool isCurrentThread() const
Query whether the current thread is active.
Definition ofThread.cpp:133
void waitForThread(bool callStopThread=true, long milliseconds=INFINITE_JOIN_TIMEOUT)
Wait for the thread to exit (aka "joining" the thread).
Definition ofThread.cpp:95
void setThreadName(const std::string &name)
Definition ofThread.cpp:33
std::thread::id getThreadId() const
Get the unique thread id.
Definition ofThread.cpp:23
bool tryLock()
Tries to lock the mutex.
Definition ofThread.cpp:80
bool isThreadRunning() const
Check the running status of the thread.
Definition ofThread.cpp:18
ofThread()
Create an ofThread.
Definition ofThread.cpp:10
@ INFINITE_JOIN_TIMEOUT
A sentinal value for an infinite join timeout.
Definition ofThread.h:272
virtual void threadedFunction()
The thread's run function.
Definition ofThread.cpp:148
OF_DEPRECATED_MSG("Use tryLock instead of setting the type of lock on startThread", void startThread(bool mutexBlocks))
Start the thread with options.
std::thread thread
The Poco::Thread that runs the Poco::Runnable.
Definition ofThread.h:316
bool lock()
Lock the mutex.
Definition ofThread.cpp:68
void unlock()
Unlock the mutex.
Definition ofThread.cpp:85
void sleep(long milliseconds)
Tell the thread to sleep for a certain amount of milliseconds.
Definition ofThread.cpp:123
std::thread & getNativeThread()
Get a reference to the underlying Poco thread.
Definition ofThread.cpp:138