Compare commits
6 Commits
dawsh
...
prerelease
Author | SHA1 | Date | |
---|---|---|---|
c991d4b3ef | |||
af819dc758 | |||
b4b476bd7f | |||
d20bda0d4e | |||
3b9ca2e144 | |||
a03eb39347 |
123
include/MultiThreading/ConcurrentQueue.h
Normal file
123
include/MultiThreading/ConcurrentQueue.h
Normal file
@@ -0,0 +1,123 @@
|
||||
#pragma once
|
||||
|
||||
#include <queue>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <type_traits>
|
||||
|
||||
/// A simple C++11 Concurrent Queue based on std::queue.
|
||||
/// Supports waiting operations for retrieving an element when it's empty.
|
||||
/// It's interrupted by calling interrupt.
|
||||
|
||||
namespace MultiThreading {
|
||||
|
||||
template<typename T, typename Container = std::deque<T> >
|
||||
class ConcurrentQueue {
|
||||
public:
|
||||
typedef typename std::queue<T>::size_type size_type;
|
||||
typedef typename std::queue<T>::reference reference;
|
||||
typedef typename std::queue<T>::const_reference const_reference;
|
||||
|
||||
~ConcurrentQueue() {
|
||||
interrupt();
|
||||
}
|
||||
|
||||
void interrupt() {
|
||||
interrupted = true;
|
||||
condition_variable.notify_one();
|
||||
}
|
||||
|
||||
bool contains(const T &value) {
|
||||
std::unique_lock<std::mutex> lock(this->mutex);
|
||||
std::queue<T, Container> copy = queue;
|
||||
while (!copy.empty())
|
||||
if (copy.front() == value)
|
||||
return true;
|
||||
else
|
||||
copy.pop();
|
||||
return false;
|
||||
}
|
||||
|
||||
void push(const T &e) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
queue.push(e);
|
||||
condition_variable.notify_one();
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void emplace(Args &&... args) {
|
||||
std::unique_lock<std::mutex> lock(this->mutex);
|
||||
queue.emplace(std::forward<Args>(args)...);
|
||||
condition_variable.notify_one();
|
||||
}
|
||||
|
||||
bool empty() {
|
||||
//std::unique_lock<std::mutex> lock(this->mutex);
|
||||
return queue.empty();
|
||||
}
|
||||
|
||||
void pop() {
|
||||
std::unique_lock<std::mutex> lock(this->mutex);
|
||||
if (!queue.empty())
|
||||
queue.pop();
|
||||
}
|
||||
|
||||
void front_pop(T &ret) {
|
||||
std::unique_lock<std::mutex> lock(this->mutex);
|
||||
wait(lock);
|
||||
ret = queue.front();
|
||||
queue.pop();
|
||||
}
|
||||
|
||||
size_type size() const {
|
||||
//std::unique_lock<std::mutex> lock(this->mutex);
|
||||
return queue.size();
|
||||
}
|
||||
|
||||
reference front() {
|
||||
std::unique_lock<std::mutex> lock(this->mutex);
|
||||
wait(lock);
|
||||
return queue.front();
|
||||
}
|
||||
|
||||
/*const_reference front() const {
|
||||
std::unique_lock<std::mutex> lock(this->mutex);
|
||||
wait(lock);
|
||||
return queue.front();
|
||||
}*/
|
||||
|
||||
reference back() {
|
||||
std::unique_lock<std::mutex> lock(this->mutex);
|
||||
wait(lock);
|
||||
return queue.back();
|
||||
}
|
||||
|
||||
/*const_reference back() const {
|
||||
std::unique_lock<std::mutex> lock(this->mutex);
|
||||
wait(lock);
|
||||
return queue.back();
|
||||
}*/
|
||||
|
||||
void swap(ConcurrentQueue &q) {
|
||||
throw std::runtime_error("Not supported");
|
||||
}
|
||||
|
||||
protected:
|
||||
std::queue<T, Container> queue;
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition_variable;
|
||||
std::atomic_bool interrupted;
|
||||
|
||||
|
||||
private:
|
||||
void wait(std::unique_lock<std::mutex> &lock) {
|
||||
interrupted = false;
|
||||
while (queue.empty()) {
|
||||
condition_variable.wait(lock);
|
||||
if (interrupted)
|
||||
throw std::runtime_error("Interrupted");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
@@ -14,13 +15,22 @@ namespace MultiThreading {
|
||||
|
||||
class MultiThreading::TaskBase {
|
||||
protected:
|
||||
std::function<void()> complete_callback = nullptr;
|
||||
std::condition_variable completed_condition;
|
||||
std::atomic<bool> complete = false;
|
||||
std::mutex mtx;
|
||||
void MarkTaskComplete() { std::lock_guard<std::mutex> lock(mtx); complete = true; completed_condition.notify_all(); }
|
||||
public:
|
||||
/// @returns whether the task is finished.
|
||||
[[nodiscard]] bool Complete() const { return complete; }
|
||||
|
||||
/// Condition variable waits for the task to be complete
|
||||
/// @note There is no way to know if the task has been assigned, If you do this and that never happens the calling thread will wait forever.
|
||||
void WaitComplete() { std::unique_lock<std::mutex> lock(mtx); completed_condition.wait(lock, [this]() { return complete.load(); }); }
|
||||
|
||||
virtual void Run() = 0;
|
||||
public:
|
||||
TaskBase() = default;
|
||||
explicit TaskBase(std::function<void()> task_complete_callback = nullptr) : complete_callback(std::move(task_complete_callback)) {};
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
@@ -29,15 +39,15 @@ private:
|
||||
T* result = nullptr;
|
||||
std::function<T()> callable = nullptr;
|
||||
private:
|
||||
explicit Task(std::function<T()> callable, T* result = nullptr) : TaskBase(), result(result), callable(callable) {}
|
||||
explicit Task(std::function<T()> callable, std::function<void()> complete_callback = nullptr, T* result = nullptr) : TaskBase(complete_callback), result(result), callable(std::move(callable)) {}
|
||||
public:
|
||||
void Run() final { result ? *result = callable() : callable(); complete = true; }
|
||||
void Run() final { result ? *result = callable() : callable(); MarkTaskComplete(); if (complete_callback) complete_callback(); }
|
||||
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 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.
|
||||
static std::shared_ptr<Task<T>> Create(std::function<T()> callable, T* result = nullptr) { return std::shared_ptr<Task<T>>(new Task<T>(callable, result)); };
|
||||
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;
|
||||
};
|
||||
|
||||
@@ -46,13 +56,13 @@ template <>
|
||||
class MultiThreading::Task<void> : public TaskBase {
|
||||
private:
|
||||
std::function<void()> callable = nullptr;
|
||||
private:
|
||||
explicit Task(std::function<void()> callable) : TaskBase(), callable(std::move(callable)) {}
|
||||
explicit Task(std::function<void()> callable, std::function<void()> complete_callback = nullptr) : TaskBase(std::move(complete_callback)), callable(std::move(callable)) {}
|
||||
public:
|
||||
void Run() final { callable(); complete = true; }
|
||||
void Run() final { callable(); MarkTaskComplete(); if (complete_callback) complete_callback(); }
|
||||
|
||||
/// 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.
|
||||
static std::shared_ptr<Task<void>> Create(const std::function<void()>& callable) { return std::shared_ptr<Task<void>>(new Task<void>(callable)); }
|
||||
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;
|
||||
};
|
@@ -1,10 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <stdexcept>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <MultiThreading/Task.h>
|
||||
|
||||
namespace MultiThreading {
|
||||
@@ -13,22 +9,39 @@ namespace MultiThreading {
|
||||
|
||||
class MultiThreading::Thread {
|
||||
private:
|
||||
std::thread worker;
|
||||
std::shared_ptr<TaskBase> current_task = nullptr;
|
||||
std::atomic<bool> busy = false;
|
||||
std::mutex mtx;
|
||||
std::condition_variable cv;
|
||||
std::atomic<bool> stop = false;
|
||||
std::condition_variable cv;
|
||||
std::thread worker;
|
||||
bool busy = false;
|
||||
std::mutex mtx;
|
||||
private:
|
||||
void Runner();
|
||||
public:
|
||||
[[nodiscard]] bool SetTask(const std::function<void()>& task);
|
||||
/// @returns false if the thread is busy.
|
||||
/// @param task The task to run.
|
||||
/// @note Task is passed by value on purpose so that the original shared_ptr to the task is still good.
|
||||
[[nodiscard]] bool SetTask(std::shared_ptr<TaskBase> task);
|
||||
|
||||
/// @returns false if the thread is busy.
|
||||
/// @param task The task to run.
|
||||
/// @note Task is passed by reference because the frame after next is going to copy it anyway.
|
||||
[[nodiscard]] bool SetTask(const std::function<void()>& task);
|
||||
|
||||
/// @returns true if the thread is currently doing work.
|
||||
/// @note SetTask will return false if the thread is busy.
|
||||
[[nodiscard]] bool Busy() const { return busy; }
|
||||
void Join() { if (worker.joinable()) worker.join(); };
|
||||
|
||||
/// Blocks the thread join is called from until execution of this thread exits.
|
||||
/// @note Joining this thread from this thread will raise an exception.
|
||||
void Join();
|
||||
public:
|
||||
/// Create a thread which will initialize and then wait for a task.
|
||||
Thread() { worker = std::thread([this] { this->Runner(); }); }
|
||||
/// Crete a thread which will immediately run a task and then wait for another.
|
||||
explicit Thread(std::shared_ptr<TaskBase> task);
|
||||
/// Crete a thread which will immediately run a task and then wait for another.
|
||||
explicit Thread(const std::function<void()>& task);
|
||||
/// Waits for the current task to finish (if there is one) and destroys the thread.
|
||||
~Thread();
|
||||
};
|
@@ -3,27 +3,51 @@
|
||||
#include <MultiThreading/Thread.h>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
namespace MultiThreading {
|
||||
class ThreadPool;
|
||||
}
|
||||
|
||||
/// A group of threads to run tasks on.
|
||||
class MultiThreading::ThreadPool {
|
||||
private:
|
||||
std::vector<MultiThreading::Thread*> threads;
|
||||
std::queue<std::shared_ptr<TaskBase>> queue;
|
||||
std::condition_variable queue_condition;
|
||||
std::mutex queue_mutex;
|
||||
private:
|
||||
/// @returns nullptr if the queue is empty.
|
||||
std::shared_ptr<TaskBase> Dequeue();
|
||||
void Runner(const std::shared_ptr<TaskBase>& task);
|
||||
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);
|
||||
|
||||
/// Set a task to be run on the thread-pool.
|
||||
/// @param task The task to run.
|
||||
void Enqueue(const std::function<void()>& task);
|
||||
public:
|
||||
/// @returns The number of threads in the thread pool.
|
||||
[[nodiscard]] unsigned int ThreadCount() const { return threads.size(); }
|
||||
[[nodiscard]] unsigned int QueueSize();
|
||||
|
||||
/// @returns the number of tasks in the queue
|
||||
/// @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.
|
||||
[[nodiscard]] bool Busy();
|
||||
|
||||
/// Uses a condition variable to wait the calling thread until the queue is empty.
|
||||
void WaitQueueEmpty();
|
||||
public:
|
||||
ThreadPool();
|
||||
explicit ThreadPool(unsigned int thread_count);
|
||||
/// Constructs a thread-pool with a given number of threads (or hardware_concurrency as the default)
|
||||
/// @param thread_count The number of threads.
|
||||
/// @note If you do a *lot* of work on the main thread and hardware_concurrency >= 2, You should use hardware_concurrency -1.
|
||||
explicit ThreadPool(unsigned int thread_count = std::thread::hardware_concurrency());
|
||||
|
||||
/// 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();
|
||||
};
|
43
main.cpp
43
main.cpp
@@ -5,13 +5,15 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace MultiThreading;
|
||||
|
||||
void cb() { std::cout << "hi" << std::endl; }
|
||||
int32_t some_test_func(int32_t hello) {
|
||||
for (unsigned int i = 0; i < 100; i++)
|
||||
std::cout << i << std::endl;
|
||||
return hello;
|
||||
for (unsigned int i = 0; i < 1000000000; i++) {}
|
||||
std::cout << "task " << hello << " finishes." << std::endl;
|
||||
return rand();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
int main() {
|
||||
// Each task you create can be run by a thread only once. It's marked as complete after.
|
||||
// If you're running a lambda or std::function directly on the thread, It can be used multiple times.
|
||||
@@ -21,8 +23,14 @@ int main() {
|
||||
// You can start threads in place like this, but you have to wait for the amount of time
|
||||
// it takes for the thread to start up which in many cases is longer than the job. Use thread pool.
|
||||
Thread some_thread;
|
||||
//some_thread.SetTaskCompletionCallback(cb);
|
||||
some_thread.SetTask(some_task);
|
||||
|
||||
//while (!some_task->Complete()) {}
|
||||
|
||||
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
std::cout << some_thread.Busy() << std::endl;
|
||||
|
||||
// Some work running concurrently with the worker thread.
|
||||
//for (unsigned int i = 0; i < 10; i++) {}
|
||||
|
||||
@@ -32,26 +40,23 @@ int main() {
|
||||
|
||||
//std::cout << a << std::endl;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
int main() {
|
||||
ThreadPool thread_pool(1);
|
||||
srand(time(nullptr));
|
||||
|
||||
auto some_task_1 = Task<void>::Create(([] { return some_test_func(1); }));
|
||||
auto some_task_2 = Task<void>::Create(([] { return some_test_func(1); }));
|
||||
auto some_task_3 = Task<void>::Create(([] { return some_test_func(1); }));
|
||||
auto some_task_4 = Task<void>::Create(([] { return some_test_func(1); }));
|
||||
int32_t task_completion_count = 0;
|
||||
auto* thread_pool = new ThreadPool(16);
|
||||
|
||||
thread_pool.Enqueue(some_task_1);
|
||||
thread_pool.Enqueue(some_task_2);
|
||||
thread_pool.Enqueue(some_task_3);
|
||||
thread_pool.Enqueue(some_task_4);
|
||||
|
||||
while (!some_task_4->Complete()) {}
|
||||
std::cout << thread_pool.ThreadCount() << std::endl;
|
||||
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);
|
||||
thread_pool->Enqueue(some_task);
|
||||
}
|
||||
|
||||
//delete thread_pool;
|
||||
}
|
||||
*/
|
||||
/// do stuff after the job.
|
||||
delete thread_pool;
|
||||
std::cout << "The returned random value was: " << task_completion_count << std::endl;
|
||||
}
|
@@ -1,25 +1,30 @@
|
||||
#include <MultiThreading/Thread.h>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
using namespace MultiThreading;
|
||||
|
||||
Thread::~Thread() {
|
||||
if (current_task)
|
||||
while (!current_task->Complete()) {}
|
||||
current_task->WaitComplete();
|
||||
stop = true;
|
||||
|
||||
cv.notify_one();
|
||||
Join();
|
||||
// Thread exits gracefully.
|
||||
}
|
||||
|
||||
bool Thread::SetTask(std::shared_ptr<TaskBase> task) {
|
||||
if (busy)
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
if (Busy())
|
||||
return false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
current_task = std::move(task);
|
||||
}
|
||||
|
||||
if (current_task)
|
||||
return false;
|
||||
|
||||
busy = true;
|
||||
current_task = std::move(task);
|
||||
|
||||
cv.notify_all();
|
||||
return true;
|
||||
}
|
||||
@@ -30,17 +35,16 @@ bool Thread::SetTask(const std::function<void()>& task) {
|
||||
|
||||
void Thread::Runner() {
|
||||
std::unique_lock<std::mutex> lock(mtx);
|
||||
|
||||
while (!stop) {
|
||||
cv.wait(lock, [this] { return stop || current_task != nullptr; });
|
||||
|
||||
if (stop)
|
||||
break;
|
||||
|
||||
busy = true;
|
||||
|
||||
lock.unlock();
|
||||
if (current_task)
|
||||
current_task->Run();
|
||||
|
||||
current_task->Run();
|
||||
|
||||
lock.lock();
|
||||
|
||||
@@ -62,3 +66,8 @@ Thread::Thread(const std::function<void()>& task) {
|
||||
if (!SetTask(task))
|
||||
throw std::runtime_error("Thread constructor failure.");
|
||||
}
|
||||
|
||||
void Thread::Join() {
|
||||
if (worker.joinable())
|
||||
worker.join();
|
||||
}
|
||||
|
@@ -3,12 +3,8 @@
|
||||
using namespace MultiThreading;
|
||||
|
||||
ThreadPool::ThreadPool(unsigned int thread_count) {
|
||||
for (unsigned int i = 0; i < thread_count; i++)
|
||||
threads.push_back(new Thread());
|
||||
}
|
||||
|
||||
ThreadPool::ThreadPool() {
|
||||
for (unsigned int i = 0; i < std::thread::hardware_concurrency(); i++)
|
||||
for (unsigned int i = 0; i < thread_count; i++)
|
||||
threads.push_back(new Thread());
|
||||
}
|
||||
|
||||
@@ -17,12 +13,12 @@ void ThreadPool::Enqueue(const std::shared_ptr<MultiThreading::TaskBase>& task)
|
||||
|
||||
// Assign it immediately if there's no wait and a thread open.
|
||||
if (queue.empty()) {
|
||||
for (auto *t: threads) {
|
||||
for (auto* t: threads) {
|
||||
if (t->Busy())
|
||||
continue;
|
||||
|
||||
if (!t->SetTask( [this, task] (){ Runner(task); } ))
|
||||
throw std::runtime_error("There was an error while setting up the task to run on the thread.");
|
||||
throw std::runtime_error("There was a collision while putting the task to the thread.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -38,6 +34,10 @@ std::shared_ptr<TaskBase> ThreadPool::Dequeue() {
|
||||
|
||||
auto task = queue.front();
|
||||
queue.pop();
|
||||
|
||||
if (queue.empty())
|
||||
queue_condition.notify_all();
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
@@ -46,25 +46,42 @@ void ThreadPool::Enqueue(const std::function<void()>& task) {
|
||||
}
|
||||
|
||||
void ThreadPool::Runner(const std::shared_ptr<TaskBase>& task) {
|
||||
if (!task->Complete())
|
||||
task->Run();
|
||||
auto running_task = task;
|
||||
|
||||
auto next_task = Dequeue();
|
||||
if (!next_task)
|
||||
return;
|
||||
Runner(next_task);
|
||||
while (running_task) {
|
||||
if (!running_task->Complete())
|
||||
running_task->Run();
|
||||
|
||||
running_task = Dequeue();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int ThreadPool::QueueSize() {
|
||||
unsigned int ThreadPool::PendingTasks() {
|
||||
std::lock_guard<std::mutex> lock(queue_mutex);
|
||||
return queue.size();
|
||||
}
|
||||
|
||||
ThreadPool::~ThreadPool() {
|
||||
// Wait for all tasks to be running.
|
||||
while (QueueSize() != 0) {}
|
||||
// Wait for all queued tasks to be running.
|
||||
WaitQueueEmpty();
|
||||
|
||||
// delete t waits for the thread to exit gracefully.
|
||||
for (auto* t: threads)
|
||||
delete t;
|
||||
}
|
||||
|
||||
bool ThreadPool::Busy() {
|
||||
if (PendingTasks() != 0)
|
||||
return true;
|
||||
|
||||
bool all_busy = true;
|
||||
for (auto* t : threads)
|
||||
if (!t->Busy()) { all_busy = false; break; }
|
||||
|
||||
return all_busy;
|
||||
}
|
||||
|
||||
void ThreadPool::WaitQueueEmpty() {
|
||||
std::unique_lock<std::mutex> lock(queue_mutex);
|
||||
queue_condition.wait(lock, [this]{ return queue.empty(); });
|
||||
}
|
||||
|
Reference in New Issue
Block a user