Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
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)
 
Result start (Function< void(Thread &)> &&func)
 Starts the new thread with given name and func.
 
void setThreadName (const native_char_t *name)
 Sets current thread name ONLY if called from inside the thread.
 
Result join ()
 Waits for thread to finish and releases its resources.
 
Result detach ()
 Detaches the thread so that its resources are automatically released back to the system without Thread::join.
 
bool wasStarted () const
 Check if thread has been started.
 

Static Public Member Functions

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

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
A native OS thread.
Definition Threading.h:118
static void Sleep(uint32_t milliseconds)
Puts current thread to sleep.
Result start(Function< void(Thread &)> &&func)
Starts the new thread with given name and func.
void setThreadName(const native_char_t *name)
Sets current thread name ONLY if called from inside the thread.
Result detach()
Detaches the thread so that its resources are automatically released back to the system without Threa...
Result join()
Waits for thread to finish and releases its resources.
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 ( )
nodiscard

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 ( )
nodiscard

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)
nodiscard

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
nodiscard

Check if thread has been started.

Returns
true if thread has been started

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