4 Commits

Author SHA1 Message Date
671e46ce66 Add operator support to collision guard 2025-07-13 16:46:34 -04:00
140902185f Update Thread.cpp
Fixed a bug that caused a race between ~Thread and Thread::Runner receiving a new job from the threadpool
2025-07-13 13:35:46 -04:00
e2b847665f Update CMakeLists.txt 2025-07-13 12:30:23 -04:00
d898a60cc1 Cleanup & Collision Guard 2025-07-13 00:48:05 -04:00
8 changed files with 210 additions and 33 deletions

View File

@@ -9,7 +9,15 @@ file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")
file(GLOB_RECURSE SOURCES "src/*.c" "src/*.cpp")
set(CMAKE_CXX_STANDARD 20)
add_library(MultiThreading SHARED ${SOURCES})
if (UNIX AND NOT APPLE)
add_library(MultiThreading SHARED ${SOURCES} "include/MultiThreading/CollisionGuard.h")
endif()
if(WIN32)
add_library(MultiThreading STATIC ${SOURCES} "include/MultiThreading/CollisionGuard.h")
endif()
target_include_directories(MultiThreading PUBLIC ${PROJECT_SOURCE_DIR}/include)
add_executable(MultiThreadingDemo main.cpp)
target_link_libraries(MultiThreadingDemo PUBLIC MultiThreading)

46
README.md Normal file
View File

@@ -0,0 +1,46 @@
# Redacted Software MultiThreading
Yet Another C++ Threading Library
[![License: Unlicense](https://img.shields.io/badge/license-Unlicense-blue.svg)](http://unlicense.org/) ![Static Badge](https://img.shields.io/badge/Lit-Based-%20)
## Features
* Easily utilize maximum parallelization of the hardware.
* Overhead < 1% compared to running functions directly.
* Thread-safe wrappers for common STL data structures.
* Public Domain Source Code.
## API Overview
## Types
* CollisionGuard
* ConcurrentQueue
* Task
* Thread
* ThreadPool
## Usage
Using a thread-pool to execute a large set of tasks, which will utilize as many hardware threads as possible.
## Requirements
* C++ 20
## Compatibility
* Linux (gcc 13.x or newer)
* Windows 10 (1909 or newer)
## Documentation
Documentation is currently limited to source-code annotations, and the demo program.
## Contributing
Contributions to MultiThreading are welcome! If you find a bug, have a feature request, or would like to contribute code, please submit an issue or pull request.
## Acknowledgements
Developed & Maintained by William @ Redacted Software and contributors.

View File

@@ -0,0 +1,113 @@
#pragma once
#include <concepts>
#include <shared_mutex>
namespace MultiThreading {
template <typename T>
class CollisionGuard;
}
template <typename T>
class MultiThreading::CollisionGuard {
private:
mutable std::shared_mutex mutex;
T value;
public:
class Read {
private:
std::shared_lock<std::shared_mutex> lock;
const T* t_ptr;
public:
Read(const std::shared_mutex& mtx, const T& val) : t_ptr(&val), lock(mtx) {}
const T* operator->() const { return t_ptr; }
};
class Write {
private:
std::unique_lock<std::shared_mutex> lock;
T* t_ptr;
public:
Write(std::shared_mutex& mtx, T& val) : t_ptr(&val), lock(mtx) {}
T* operator->() { return t_ptr; }
};
public:
Read operator->() const { return Read(mutex, value); }
Write operator->() { return Write(mutex, value); }
public:
template <typename U = T>
requires requires(const U& a, const U& b) { a + b; }
T operator +(const T& rhs) const {
std::shared_lock lock(mutex);
return value + rhs;
}
template <typename U = T>
requires requires(U& a, const U& b) { a = b; }
CollisionGuard& operator =(const T& rhs) {
std::unique_lock lock(mutex);
value = rhs;
return *this;
}
explicit operator T() const requires std::copy_constructible<T> {
std::shared_lock lock(mutex);
return value;
}
template <typename U = T>
requires requires(U& a, const U& b) { a += b; }
CollisionGuard& operator +=(const T& rhs) {
std::unique_lock lock(mutex);
value += rhs;
return *this;
}
template <typename U = T>
requires requires(U& a, const U& b) { a -= b; }
CollisionGuard& operator -=(const T& rhs) {
std::unique_lock lock(mutex);
value -= rhs;
return *this;
}
template <typename U = T>
requires requires(U& a, const U& b) { a *= b; }
CollisionGuard& operator *=(const T& rhs) {
std::unique_lock lock(mutex);
value *= rhs;
return *this;
}
template <typename U = T>
requires requires(U& a, const U& b) { a /= b; }
CollisionGuard& operator /=(const T& rhs) {
std::unique_lock lock(mutex);
value /= rhs;
return *this;
}
template <typename U = T>
requires requires(const U& a, const U& b) { a - b; }
T operator -(const T& rhs) const {
std::shared_lock lock(mutex);
return value - rhs;
}
template <typename U = T>
requires requires(const U& a, const U& b) { a * b; }
T operator *(const T& rhs) const {
std::shared_lock lock(mutex);
return value * rhs;
}
template <typename U = T>
requires requires(const U& a, const U& b) { a / b; }
T operator /(const T& rhs) const {
std::shared_lock lock(mutex);
return value / rhs;
}
public:
template <typename... Args>
explicit CollisionGuard(Args&&... args) : value(std::forward<Args>(args)...) {}
};

View File

@@ -20,6 +20,8 @@ protected:
std::atomic<bool> complete = false;
std::mutex mtx;
void MarkTaskComplete() { std::lock_guard<std::mutex> lock(mtx); complete = true; completed_condition.notify_all(); }
protected:
explicit TaskBase(std::function<void()> task_complete_callback = nullptr) : complete_callback(std::move(task_complete_callback)) {};
public:
/// @returns whether the task is finished.
[[nodiscard]] bool Complete() const { return complete; }
@@ -29,8 +31,6 @@ public:
void WaitComplete() { std::unique_lock<std::mutex> lock(mtx); completed_condition.wait(lock, [this]() { return complete.load(); }); }
virtual void Run() = 0;
public:
explicit TaskBase(std::function<void()> task_complete_callback = nullptr) : complete_callback(std::move(task_complete_callback)) {};
};
template <typename T>
@@ -45,8 +45,10 @@ public:
public:
/// Create a Task. This is non-negotiable, This ensures that there's no issues related to the stack frame the task is on being popped before or while the job runs.
/// @param callable The function to run, *usually a lambda or std::bind*
/// @param complete_callback A void callable to be run after the task.
/// @param result If your function returns a value, This is where you want it to go. nullptr for no return value or you don't want it.
/// @note this is shared_ptr so you don't have to delete it.
/// @note If result is stack allocated your program will probably explode.
/// @note this is shared_ptr so that you don't have to delete it.
static std::shared_ptr<Task<T>> Create(std::function<T()> callable, const std::function<void()>& complete_callback = nullptr, T* result = nullptr) { return std::shared_ptr<Task<T>>(new Task<T>(callable, complete_callback, result)); };
~Task() = default;
};
@@ -62,7 +64,8 @@ public:
/// Create a Task. This is non-negotiable, This ensures that there's no issues related to the stack frame the task is on being popped before or while the job runs.
/// @param callable The function to run, *usually a lambda or std::bind*
/// @note this is shared_ptr so you don't have to delete it.
/// @param complete_callback A void callable to be run after the task.
/// @note this is shared_ptr so that you don't have to delete it.
static std::shared_ptr<Task<void>> Create(const std::function<void()>& callable, const std::function<void()>& complete_callback = nullptr) { return std::shared_ptr<Task<void>>(new Task<void>(callable, complete_callback)); }
~Task() = default;
};

View File

@@ -15,6 +15,7 @@ private:
std::queue<std::shared_ptr<TaskBase>> queue;
std::condition_variable queue_condition;
std::mutex queue_mutex;
bool destructing = false;
private:
/// @returns nullptr if the queue is empty.
std::shared_ptr<TaskBase> Dequeue();
@@ -22,11 +23,11 @@ private:
public:
/// Set a task to be run on the thread-pool.
/// @param task The task to run.
void Enqueue(const std::shared_ptr<MultiThreading::TaskBase>& task);
bool Enqueue(const std::shared_ptr<MultiThreading::TaskBase>& task);
/// Set a task to be run on the thread-pool.
/// @param task The task to run.
void Enqueue(const std::function<void()>& task);
bool Enqueue(const std::function<void()>& task);
public:
/// @returns The number of threads in the thread pool.
[[nodiscard]] unsigned int ThreadCount() const { return threads.size(); }
@@ -35,7 +36,7 @@ public:
/// @note this excludes the tasks currently being run.
[[nodiscard]] unsigned int PendingTasks();
/// @returns Whether a task you enqueue would have to wait for something else to finish.
/// @returns Whether a task you enqueue would have to wait for something else to finish before running.
[[nodiscard]] bool Busy();
/// Uses a condition variable to wait the calling thread until the queue is empty.
@@ -48,6 +49,5 @@ public:
/// Waits for the threads to empty the queue and destroys the thread pool.
/// @note This should be one of the very last things your program does before exiting.
// TODO make Enqueue fail during destruction to avoid stalls.
~ThreadPool();
};

View File

@@ -1,16 +1,15 @@
#include <MultiThreading/Task.h>
#include <MultiThreading/Thread.h>
#include <MultiThreading/ThreadPool.h>
#include <MultiThreading/CollisionGuard.h>
#include <iostream>
using namespace MultiThreading;
void cb() { std::cout << "hi" << std::endl; }
int32_t some_test_func(int32_t hello) {
//void cb() { std::cout << "hi" << std::endl; }
void some_test_func() {
for (unsigned int i = 0; i < 1000000000; i++) {}
std::cout << "task " << hello << " finishes." << std::endl;
return rand();
std::cout << "task finishes." << std::endl;
}
/*
@@ -42,21 +41,16 @@ int main() {
}
*/
int main() {
srand(time(nullptr));
int32_t task_completion_count = 0;
auto* thread_pool = new ThreadPool(16);
CollisionGuard<int> task_completion_count(0);
auto* thread_pool = new ThreadPool();
for (unsigned int i = 0; i < 128; i++) {
auto some_task = Task<int32_t>::Create(([i] { return some_test_func(i); }), cb, &task_completion_count);
auto some_task = Task<void>::Create([&task_completion_count]() { task_completion_count += 1; }, [&task_completion_count]() { task_completion_count += 1; });
thread_pool->Enqueue(some_task);
}
/// do stuff after the job.
delete thread_pool;
std::cout << "The returned random value was: " << task_completion_count << std::endl;
std::cout << "The number of tasks run was " << (int) task_completion_count << std::endl;
}

View File

@@ -1,16 +1,23 @@
#include <MultiThreading/Thread.h>
#include <utility>
#include <MultiThreading/Thread.h>
using namespace MultiThreading;
Thread::~Thread() {
if (current_task)
current_task->WaitComplete();
stop = true;
std::shared_ptr<TaskBase> task;
{
std::lock_guard<std::mutex> lock(mtx);
task = current_task;
}
if (task)
task->WaitComplete();
stop = true;
cv.notify_one();
Join();
// Thread exits gracefully.
}
bool Thread::SetTask(std::shared_ptr<TaskBase> task) {
@@ -42,9 +49,10 @@ void Thread::Runner() {
if (stop)
break;
auto task = current_task;
lock.unlock();
current_task->Run();
task->Run();
lock.lock();

View File

@@ -8,7 +8,10 @@ ThreadPool::ThreadPool(unsigned int thread_count) {
threads.push_back(new Thread());
}
void ThreadPool::Enqueue(const std::shared_ptr<MultiThreading::TaskBase>& task) {
bool ThreadPool::Enqueue(const std::shared_ptr<MultiThreading::TaskBase>& task) {
if (destructing)
return false;
std::lock_guard<std::mutex> lock(queue_mutex);
// Assign it immediately if there's no wait and a thread open.
@@ -19,12 +22,13 @@ void ThreadPool::Enqueue(const std::shared_ptr<MultiThreading::TaskBase>& task)
if (!t->SetTask( [this, task] (){ Runner(task); } ))
throw std::runtime_error("There was a collision while putting the task to the thread.");
return;
return true;
}
}
// Alternatively it goes in-to the queue.
queue.push(task);
return true;
}
std::shared_ptr<TaskBase> ThreadPool::Dequeue() {
@@ -41,8 +45,8 @@ std::shared_ptr<TaskBase> ThreadPool::Dequeue() {
return task;
}
void ThreadPool::Enqueue(const std::function<void()>& task) {
Enqueue(Task<void>::Create(task));
bool ThreadPool::Enqueue(const std::function<void()>& task) {
return Enqueue(Task<void>::Create(task));
}
void ThreadPool::Runner(const std::shared_ptr<TaskBase>& task) {
@@ -62,6 +66,7 @@ unsigned int ThreadPool::PendingTasks() {
}
ThreadPool::~ThreadPool() {
destructing = true;
// Wait for all queued tasks to be running.
WaitQueueEmpty();