Basic thread template. The ThreadInterface is derived from JobInterface but has its own private thread and therefore you can immediately start it using Start() or Run().
One way to create a thread is to inherit from ThreadInterface/ThreadInterfaceTemplate and to implement GetName() and operator (), for example
class MyThread : public ThreadInterfaceTemplate<MyThread>
{
public:
const char*
GetName()
const {
return "MyThread"; }
Result<void> operator ()()
{
... your code goes here ...
... don't forget to check IsCancelled() and return OK or an error ...
}
};
const Char * GetName() const
Definition: job.h:179
A thread is reference counted. If you all you want to do is start it, you can create it with NewObj(), check the return value and call Start(). This will automatically delete your thread object once it finished and is not referenced anymore. If your thread performs potentially lengthy operations in a loop it must call IsCancelled() periodically.
Threads are reference-counted and you must not create an instance on the stack or as member variable of a class. You can create a thread using NewObj or via ThreadRef::Run() or ThreadRef::Create().
Waits until this thread has been executed. As long as a thread hasn't been started it is considered not to have finished yet. Once it has run this will return immediately until you restart the thread.
Wait() might execute other jobs in the current queue until the one you are waiting for has finished or is timed out. Therefore you may never lock a shared resource another job might use as well and then wait. For one this could dead-lock and conceptually this would result in single-threaded performance.
If you call Wait() from within an enqueued job you must have started what you are waiting for. Otherwise Wait() will immediately return false because this would lead to a deadlock. The same applies if a thread tries to wait for itself.
Instead of waiting for a thread to start some action after it has finished you can subscribe to ObservableFinished(). This cannot dead-lock, is more efficient and can even be used to run the observer in a different queue. For example you can run a thread and register an observer for it that will run on the main thread's queue and updates the UI. THREADSAFE.
- Parameters
-
[in] | timeout | Maximum wait interval (or TIMEVALUE_INFINITE for no time-out). |
[in] | mode | WAITMODE::DEFAULT by default. WAITMODE::RETURN_ON_CANCEL means that Wait() will return if the caller has been cancelled even if the condition has not been set yet. |
- Returns
- True if successful, false if you try to wait inside an enqueued job.