[file:
Namespace: | dmThread |
Include: | #include <dmsdk/dlib/thread.h> |
TYPES | |
---|---|
ThreadStart |
FUNCTIONS | |
---|---|
dmThread::TlsKey AllocTls() | allocate thread local storage key |
void Detach(dmThread::Thread thread) | detach thread |
void FreeTls(dmThread::TlsKey key) | free thread local storage key |
dmThread::Thread GetCurrentThread() | gets the current thread |
void GetTlsValue(dmThread::TlsKey key) | get thread specific data |
void Join(dmThread::Thread thread) | join thread |
dmThread::Thread New(ThreadStart thread_start, uint32_t stack_size, void* arg, const char* name) | create a new thread |
void SetThreadName(dmThread::Thread thread, const char* name) | sets the current thread name |
void SetTlsValue(dmThread::TlsKey key, void* value) | set thread specific data |
dmThread::TlsKey AllocTls()
Allocate thread local storage key
PARAMETERS
RETURNS
dmThread::TlsKey |
Key |
void Detach(dmThread::Thread thread)
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
dmThread::Thread |
thread |
Thread to detach |
void FreeTls(dmThread::TlsKey key)
Free thread local storage key
PARAMETERS
dmThread::TlsKey |
key |
Key |
dmThread::Thread GetCurrentThread()
Gets the current thread
PARAMETERS
RETURNS
dmThread::Thread |
the current thread |
void GetTlsValue(dmThread::TlsKey key)
Get thread specific data
PARAMETERS
dmThread::TlsKey |
key |
Key |
void Join(dmThread::Thread thread)
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
dmThread::Thread |
thread |
Thread to join |
dmThread::Thread New(ThreadStart thread_start, uint32_t stack_size, void* arg, const char* name)
Create a new named thread
PARAMETERS
ThreadStart |
thread_start |
Thread entry function |
uint32_t |
stack_size |
Stack size |
void* |
arg |
Thread argument |
const char* |
name |
Thread name |
RETURNS
dmThread::Thread |
Thread handle |
EXAMPLES
Create a thread#include <stdio.h>
#include <dmsdk/dlib/thread.h>
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);
}
void SetThreadName(dmThread::Thread thread, const char* name)
Sets the current thread name
PARAMETERS
dmThread::Thread |
thread |
the thread |
const char* |
name |
the thread name |
void SetTlsValue(dmThread::TlsKey key, void* value)
Set thread specific data
PARAMETERS
dmThread::TlsKey |
key |
Key |
void* |
value |
Value |