🟥 Atomic, thread, thread pool, mutex, condition variable
Threading is a library defining basic primitives for user-space threading and synchronization.
Features
Class | Description |
SC::Thread | A native OS thread. |
SC::ThreadPool | Simple thread pool that executes tasks in a fixed number of worker threads. |
SC::Mutex | A native OS mutex to synchronize access to shared resources. |
SC::ConditionVariable | A native OS condition variable. |
SC::Atomic | Atomic variables (only for int and bool for now). |
SC::EventObject | An automatically reset event object to synchronize two threads. |
Status
🟥 Draft
Only the features needed for other libraries have been implemented so far. The Atomic header is really only being implemented for a few data types and needs some love to extend and improve it.
Description
SC::Thread
A native OS thread. Example:
Thread thread;
thread.start([](Thread& thread)
{
thread.setThreadName(SC_NATIVE_STR("My Thread"));
Thread::Sleep(1000);
});
thread.join();
thread.detach();
- Warning
- Thread destructor will assert if SC::Thread::detach() or SC::Thread::join() has not been called.
SC::ThreadPool
Simple thread pool that executes tasks in a fixed number of worker threads. This class is not copyable / moveable due to it containing Mutex and Condition variable. Additionally, this class does not allocate any memory by itself, and expects the caller to supply SC::ThreadPool::Task objects.
- Warning
- The caller is responsible of keeping Task address stable until the it will be completed. If it's not already completed the task must still be valid during ThreadPool::destroy or ThreadPool destructor.
Example:
static const size_t wantedThreads = 4;
static const size_t numTasks = 100;
size_t values[numTasks];
for (size_t idx = 0; idx < numTasks; idx++)
{
size_t* value = values + idx;
*value = idx;
{
if (*value % 2)
{
}
*value *= 100;
};
}
bool allGood = true;
for (size_t idx = 0; idx < numTasks; idx++)
{
allGood = allGood && (values[idx] == idx * 100);
}
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113
static void Sleep(uint32_t milliseconds)
Puts current thread to sleep.
Simple thread pool that executes tasks in a fixed number of worker threads.
Definition: ThreadPool.h:41
Result destroy()
Destroy the thread pool created previously with ThreadPool::create.
Result queueTask(Task &task)
Queue a task (that should not be already in use)
Result waitForTask(Task &task)
Blocks execution until all queued and pending tasks will be fully completed.
Result create(size_t workerThreads)
Create a thread pool with the requested number of worker threads.
Result waitForAllTasks()
Blocks execution until all queued and pending tasks will be fully completed.
A small task containing a function to execute that can be queued in the thread pool.
Definition: ThreadPool.h:19
Function< void()> function
Function that will be executed during the task.
Definition: ThreadPool.h:21
SC::Mutex
A native OS mutex to synchronize access to shared resources. Example:
Mutex mutex;
int globalVariable = 0;
Thread thread1;
auto thread1Func = [&](Thread& thread)
{
thread.setThreadName(SC_NATIVE_STR("Thread1"));
mutex.lock();
globalVariable++;
mutex.unlock();
};
Thread thread2;
auto thread2Func = [&](Thread& thread)
{
thread.setThreadName(SC_NATIVE_STR("Signaling2"));
mutex.lock();
globalVariable++;
mutex.unlock();
};
SC::EventObject
An automatically reset event object to synchronize two threads.
Example:
EventObject eventObject;
Thread threadWaiting;
auto waitingFunc = [&](Thread& thread)
{
thread.setThreadName(SC_NATIVE_STR("Thread waiting"));
eventObject.wait();
report.console.printLine("After waiting");
};
Thread threadSignaling;
auto signalingFunc = [&](Thread& thread)
{
thread.setThreadName(SC_NATIVE_STR("Signaling thread"));
report.console.printLine("Signal");
eventObject.signal();
};
SC::Atomic
Atomic variables (only for int
and bool
for now).
Example:
Atomic<bool> test = true;
test.exchange(false);
Roadmap
🟨 MVP
🟩 Usable
🟦 Complete Features:
- Support more types in Atomic<T>
- ReadWrite Lock
- Barrier