-
首页
-
C4D R23.110 C++ SDK
ThreadInterface Class Reference
线程
#include <thread.h>
详细描述
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()
[1/2]
-
参数
-
[in]
|
jmpTable
|
Jump table of the implementation class.
|
◆
ThreadInterface()
[2/2]
成员函数文档编制
◆
IsRunning()
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()
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()
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.
-
参数
-
-
返回
-
OK on success. Failse if the thread is already running or no more threads are available.
◆
GetCurrentThread()
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]
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()
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
|
(
|
|
)
|
|
const Char * GetName() const
定义:
job.h:180