Skip to content

Instantly share code, notes, and snippets.

@unixnut
Last active May 20, 2025 16:06
Show Gist options
  • Select an option

  • Save unixnut/f437853136696cb615df3d0a98594c01 to your computer and use it in GitHub Desktop.

Select an option

Save unixnut/f437853136696cb615df3d0a98594c01 to your computer and use it in GitHub Desktop.
Thread Pool by CppNuts
*~
*.[oa]
*.l[ao]
.deps
.libs
#include <iostream>
#include <thread>
#include <sstream>
#include "ThreadPool.h"
std::string get_thread_id()
{
auto myid = std::this_thread::get_id();
std::stringstream ss;
ss << myid;
std::string mystr = ss.str();
return mystr;
}
int main()
{
ThreadPool pool(4); // Create a pool with N number of worker threads
std::cout << "Thread Pool Created\n";
std::cout << "Enqueue (Assign) some tasks \n";
for (int i = 0; i < 8; ++i)
{
pool.enqueue([i] {
printf("Task %d %s executed by thread \n", i, get_thread_id().c_str());
std::this_thread::sleep_for(std::chrono::seconds(1)); // Simulate some work
});
}
// Main thread continues doing other things
// while the tasks are executed in the background
return 0;
}
~/bin/thread_pool: demo.o ThreadPool.o
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -lpthread -o $@
demo.o ThreadPool.o: ThreadPool.h
%.o: %.c++
# recipe to execute (built-in):
$(COMPILE.cpp) --std=c++17 $(OUTPUT_OPTION) $<
#include "ThreadPool.h"
ThreadPool::ThreadPool(size_t numThreads) : stop(false)
{
for (size_t i = 0; i < numThreads; ++i)
{
workers.emplace_back(&ThreadPool::run, this);
}
}
ThreadPool::~ThreadPool()
{
std::unique_lock<std::mutex> lock(queueMutex);
stop = true;
lock.unlock();
condition.notify_all();
for (std::thread& worker : workers)
worker.join();
}
void ThreadPool::run()
{
for (;;)
{
std::unique_lock<std::mutex> lock(queueMutex);
condition.wait(lock, [this] { return stop || !tasks.empty(); });
if (stop && tasks.empty())
return;
auto task = std::move(tasks.front()); // Excract task from tasks list.
tasks.pop(); // Remove task from list as going to execute it.
lock.unlock(); // Unlock mutex, so another thread can accept the tasks.
task(); // Run The Task
}
}
#include <vector>
#include <queue>
#include <thread>
#include <functional>
#include <mutex>
#include <condition_variable>
class ThreadPool
{
public:
ThreadPool(size_t numThreads);
template<class F>
void enqueue(F&& task)
{
std::unique_lock<std::mutex> lock(queueMutex);
tasks.emplace(std::forward<F>(task));
lock.unlock();
condition.notify_one();
}
~ThreadPool();
protected:
void run();
private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex queueMutex;
std::condition_variable condition;
bool stop;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment