Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::Thread Struct Reference

A native OS thread. More...

#include <Threading.h>

Public Member Functions

 Thread (Thread &&)=delete
 
Threadoperator= (Thread &&)=delete
 
 Thread (const Thread &)=delete
 
Threadoperator= (const Thread &)=delete
 
uint64_t threadID ()
 Returns thread id of this thread object (not current thread) More...
 
Result start (Function< void(Thread &)> &&func)
 Starts the new thread with given name and func. More...
 
void setThreadName (const native_char_t *name)
 Sets current thread name ONLY if called from inside the thread. More...
 
Result join ()
 Waits for thread to finish and releases its resources. More...
 
Result detach ()
 Detaches the thread so that its resources are automatically released back to the system without Thread::join. More...
 
bool wasStarted () const
 Check if thread has been started. More...
 

Static Public Member Functions

static uint64_t CurrentThreadID ()
 Returns thread id of the thread calling the function. More...
 
static void Sleep (uint32_t milliseconds)
 Puts current thread to sleep. More...
 

Detailed Description

A native OS thread.

Example:

Thread thread;
thread.start([](Thread& thread)
{
// It's highly recommended setting a name for the thread
thread.setThreadName(SC_NATIVE_STR("My Thread"));
// Do something on the thread
Thread::Sleep(1000); // Sleep for 1 second
});
thread.join(); // wait until thread has finished executing
// ...or
thread.detach(); // To keep thread running after Thread destructor
static void Sleep(uint32_t milliseconds)
Puts current thread to sleep.
Warning
Thread destructor will assert if SC::Thread::detach() or SC::Thread::join() has not been called.

Member Function Documentation

◆ CurrentThreadID()

static uint64_t SC::Thread::CurrentThreadID ( )
static

Returns thread id of the thread calling the function.

Returns
thread id

◆ detach()

Result SC::Thread::detach ( )

Detaches the thread so that its resources are automatically released back to the system without Thread::join.

Returns
Valid Result if thread has been detached

◆ join()

Result SC::Thread::join ( )

Waits for thread to finish and releases its resources.

Returns
Valid Result if thread has finished

◆ setThreadName()

void SC::Thread::setThreadName ( const native_char_t name)

Sets current thread name ONLY if called from inside the thread.

Parameters
nameThe name of the thread
Warning
This function will ASSERT if it's not called from the thread itself.

◆ Sleep()

static void SC::Thread::Sleep ( uint32_t  milliseconds)
static

Puts current thread to sleep.

Parameters
millisecondsSleep for given number of milliseconds

◆ start()

Result SC::Thread::start ( Function< void(Thread &)> &&  func)

Starts the new thread with given name and func.

Parameters
funcFunction running on thread. Must be a valid pointer to action for the entire duration of thread.

◆ threadID()

uint64_t SC::Thread::threadID ( )

Returns thread id of this thread object (not current thread)

◆ wasStarted()

bool SC::Thread::wasStarted ( ) const

Check if thread has been started.

Returns
true if thread has been started

The documentation for this struct was generated from the following file: