ThreadInterface Class Reference 线程

#include <thread.h>

Inheritance diagram for ThreadInterface:

详细描述

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 ... } };

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() .

公共成员函数

  ThreadInterface (const JobInterfaceJumpTablePOD &jmpTable)
  ThreadInterface ( ThreadInterface && src )
Bool   IsRunning () const
Bool   Wait ( TimeValue timeout= TIMEVALUE_INFINITE , WAITMODE mode= WAITMODE::DEFAULT ) const
Result < void >  开始 ( THREADPRIORITY priority= THREADPRIORITY::NORMAL )
String   ToString (const FormatStatement *formatStatement=nullptr) const
Result < void >  PrivateResetState ()
-  Public Member Functions inherited from JobInterface
JOBOPTIONFLAGS  GetJobOptions () const
const Char GetName () const
  JobInterface (const JobInterfaceJumpTablePOD &jmpTable)
  ~JobInterface ()
  JobInterface ( JobInterface && src )
JobInterface operator= ( JobInterface && src )
Bool   Wait ( TimeValue timeout= TIMEVALUE_INFINITE , WAITMODE mode= WAITMODE::DEFAULT ) const
Result < void >  GetResult ( TimeValue timeout= TIMEVALUE_INFINITE , WAITMODE mode= WAITMODE::DEFAULT ) const
Result < void >  MoveResult ( TimeValue timeout= TIMEVALUE_INFINITE , WAITMODE mode= WAITMODE::DEFAULT )
void  取消 ()
Bool   IsCancelled () const
void  CancelAndWait ( WAITMODE mode= WAITMODE::DEFAULT )
JobInterface Enqueue ( JobQueueInterface *queue= JOBQUEUE_CURRENT )
Result < void >  AddSubJob ( JobInterface *subJob)
template<typename JOB >
Result < void >  AddSubJob ( ResultMemT < JOB * > subJob)
template<typename JOBREF >
Result < void >  AddSubJob ( ResultMemT < JOBREF > &&subJob)
template<typename GROUP >
Result < void >  AddSubGroup (GROUP *subGroup)
template<typename GROUP >
Result < void >  AddSubGroup ( ResultMemT < GROUP * > subGroup)
template<typename GROUP >
Result < void >  AddSubGroup ( ResultMemT < GROUP > subGroup)
JobGroupInterface GetJobGroup () const
ObservableFinishedBase < JobInterface ObservableFinished ()
ObservableCancelledBase < JobInterface ObservableCancelled ()
String   ToString (const FormatStatement *formatStatement=nullptr) const

静态公共成员函数

static const ThreadInterface GetCurrentThread ()
static Bool   IsMainThread ()
static THREADTYPE   GetCurrentThreadType ()
static THREADTYPE   GetCurrentThreadType ( Int &threadIdentifier)
static Result < void >  AssimilateAlienThread ()
-  Static Public Member Functions inherited from JobInterface
static Int   GetCurrentWorkerThreadIndex ()
static Int   GetCurrentThreadCount ()
static Bool   IsCurrentJobCancelled (const JobInterface *optionalJob=nullptr)
static JobStatusInterface GetCurrentJob ()

构造函数 & 析构函数文档编制

◆  ThreadInterface() [1/2]

ThreadInterface ( const JobInterfaceJumpTablePOD &  jmpTable )
explicit
参数
[in] jmpTable Jump table of the implementation class.

◆  ThreadInterface() [2/2]

ThreadInterface ( ThreadInterface &&  src )

成员函数文档编制

◆  IsRunning()

Bool IsRunning ( ) const

Checks whether this thread is currently running. 使用 Wait() if you have to wait for a thread to finish. Repeatedly calling this method will be detected and result in a debug break. THREADSAFE.

返回
False if the thread wasn't running anymore.

◆  Wait()

Bool Wait ( TimeValue   timeout = TIMEVALUE_INFINITE ,
WAITMODE   mode = WAITMODE::DEFAULT  
) const

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.

参数
[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.
返回
True if successful, false if you try to wait inside an enqueued job.

◆  Start()

Result <void> Start ( THREADPRIORITY   priority = THREADPRIORITY::NORMAL )

Starts a thread to execute this job's worker method (will add a reference and remove it when the object is no longer needed). If you try to start an already running thread this will fail and return an error.

参数
[in] priority THREADPRIORITY::NORMAL or THREADPRIORITY::BELOW for a background thread.
返回
OK on success. Failse if the thread is already running or no more threads are available.

◆  GetCurrentThread()

static const ThreadInterface * GetCurrentThread ( )
static

Returns a pointer to the currently running thread. If you call this from a job or a thread you have created by using OS APIs a nullptr is returned. THREADSAFE.

返回
This thread's ThreadInterface* or nullptr (worker, main or other OS thread)

◆  IsMainThread()

static Bool IsMainThread ( )
static

Checks if this thread is the main application thread. THREADSAFE.

返回
True if this is the main application thread.

◆  GetCurrentThreadType() [1/2]

static THREADTYPE GetCurrentThreadType ( )
static

Returns information about the current thread. THREADSAFE.

返回
See THREADTYPE.

◆  GetCurrentThreadType() [2/2]

static THREADTYPE GetCurrentThreadType ( Int threadIdentifier )
static

Returns information about the current thread. THREADSAFE.

参数
[out] threadIdentifier For THREADTYPE::WORKER the the index of the worker thread is returned, for THREADTYPE::MAIN 0 is returned if it currently executes jobs. Otherwise this is an opaque identifier for the current thread.
返回
See THREADTYPE.

◆  AssimilateAlienThread()

static Result <void> AssimilateAlienThread ( )
static

Allocates internal resources for an alien thread (CoreThread, unique thread index and so on). THREADSAFE.

返回
OK on success.

◆  ToString()

String ToString ( const FormatStatement formatStatement = nullptr ) const

Returns a readable string of the content.

参数
[in] formatStatement Nullptr or additional formatting instruction. Currently no additional formatting instructions are supported.
返回
The converted result.

◆  PrivateResetState()

Result <void> PrivateResetState ( )
maxon::JobInterface::GetName
const Char * GetName() const
定义: job.h:180