Thread

[file:] Thread functions.

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

Functions

AllocTls

dmThread::TlsKey AllocTls()

Allocate thread local storage key

PARAMETERS

RETURNS

dmThread::TlsKey Key

Detach

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

FreeTls

void FreeTls(dmThread::TlsKey key)

Free thread local storage key

PARAMETERS

dmThread::TlsKey key Key

GetCurrentThread

dmThread::Thread GetCurrentThread()

Gets the current thread

PARAMETERS

RETURNS

dmThread::Thread the current thread

GetTlsValue

void GetTlsValue(dmThread::TlsKey key)

Get thread specific data

PARAMETERS

dmThread::TlsKey key Key

Join

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

New

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

SetThreadName

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

SetTlsValue

void SetTlsValue(dmThread::TlsKey key, void* value)

Set thread specific data

PARAMETERS

dmThread::TlsKey key Key
void* value Value

Types

ThreadStart