Sdk thread api documentation

Thread functions.

Namespace: dmThread
Include: #include <dmsdk/dlib/thread.h>
FUNCTIONS
 dmThread::AllocTls() allocate thread local storage key
void dmThread::Detach( thread) detach thread
void dmThread::FreeTls( key) free thread local storage key
 dmThread::GetCurrentThread() gets the current thread
void dmThread::GetTlsValue( key) get thread specific data
void dmThread::Join( thread) join thread
 dmThread::New( thread_start,  stack_size,  arg,  name) create a new thread
void dmThread::SetThreadName( thread,  name) sets the current thread name
void dmThread::SetTlsValue( key,  value) set thread specific data

Functions

dmThread::AllocTls

 dmThread::AllocTls()

Allocate thread local storage key

PARAMETERS

RETURNS


dmThread::Detach

void dmThread::Detach( 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

thread Thread to detach

dmThread::FreeTls

void dmThread::FreeTls( key)

Free thread local storage key

PARAMETERS

key Key

dmThread::GetCurrentThread

 dmThread::GetCurrentThread()

Gets the current thread

PARAMETERS

RETURNS

current thread

dmThread::GetTlsValue

void dmThread::GetTlsValue( key)

Get thread specific data

PARAMETERS

key Key

dmThread::Join

void dmThread::Join( 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

thread Thread to join

dmThread::New

 dmThread::New( thread_start,  stack_size,  arg,  name)

Create a new named thread

PARAMETERS

thread_start Thread entry function
stack_size Stack size
arg Thread argument
name Thread name

RETURNS

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

dmThread::SetThreadName

void dmThread::SetThreadName( thread,  name)

Sets the current thread name

PARAMETERS

thread the thread
name the thread name

dmThread::SetTlsValue

void dmThread::SetTlsValue( key,  value)

Set thread specific data

PARAMETERS

key Key
value Value