Namespace: dmThread
Language: C++
Type: Defold C++
File: thread.h
Source: engine/dlib/src/dmsdk/dlib/thread.h
Include: dmsdk/dlib/thread.h
Thread functions.
Type: FUNCTION Allocate thread local storage key
Returns
key (dmThread::TlsKey) - KeyType: FUNCTION Detach thread. When a detached thread terminates, its resources are automatically released back to the system without the need for another thread to join with the terminated thread.
Parameters
thread (dmThread::Thread) - Thread to detachType: FUNCTION Free thread local storage key
Parameters
key (dmThread::TlsKey) - KeyType: FUNCTION Gets the current thread
Returns
thread (dmThread::Thread) - the current threadType: FUNCTION Get thread specific data
Parameters
key (dmThread::TlsKey) - KeyType: FUNCTION Join thread. Waits for the thread specified by thread to terminate. If that thread has already terminated, then Join() returns immediately. The thread specified by thread must be joinable (see Detach()).
Parameters
thread (dmThread::Thread) - Thread to joinType: FUNCTION Create a new named thread
Notes
Parameters
thread_start (ThreadStart) - Thread entry functionstack_size (uint32_t) - Stack sizearg (void*) - Thread argumentname (const char*) - Thread nameReturns
thread (dmThread::Thread) - Thread handleExamples
Create a thread
#include
#include
struct Context
{
bool m_DoWork;
int m_Work;
};
static void Worker(void* _ctx)
{
Context* ctx = (Context*)_ctx;
while (ctx->m_DoWork)
{
ctx->m_Work++; // do work
dmTime::Sleep(10*1000); // yield
}
}
int StartThread()
{
Context ctx;
ctx.m_DoWork = true;
ctx.m_Work = 0;
dmThread::Thread thread = dmThread::New(Worker, 0x80000, (void*)&ctx, "my_thread");
// do other work...
// ..eventually stop the thread:
ctx.m_DoWork = false;
// wait for thread
dmThread::Join(thread);
printf("work done: %d\n", ctx.m_Work);
}
Type: FUNCTION Sets the current thread name
Notes
Parameters
thread (dmThread::Thread) - the threadname (const char*) - the thread nameType: FUNCTION Set thread specific data
Parameters
key (dmThread::TlsKey) - Keyvalue (void*) - ValueType: TYPEDEF