{"id":20590237,"url":"https://github.com/corpssansorganes/thread_pool","last_synced_at":"2026-06-01T04:31:40.919Z","repository":{"id":158906187,"uuid":"634308857","full_name":"CorpsSansOrganes/thread_pool","owner":"CorpsSansOrganes","description":"C++11 thread pool, designed for ease of use.","archived":false,"fork":false,"pushed_at":"2024-07-01T16:09:39.000Z","size":352,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-06T13:41:11.872Z","etag":null,"topics":["cpp","cpp11","semaphore","thread-pool","waitable-queue"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CorpsSansOrganes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-29T17:53:29.000Z","updated_at":"2024-07-01T16:09:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"3476d5b2-4295-4913-a37a-52fb354c0ac0","html_url":"https://github.com/CorpsSansOrganes/thread_pool","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/CorpsSansOrganes/thread_pool","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorpsSansOrganes%2Fthread_pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorpsSansOrganes%2Fthread_pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorpsSansOrganes%2Fthread_pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorpsSansOrganes%2Fthread_pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CorpsSansOrganes","download_url":"https://codeload.github.com/CorpsSansOrganes/thread_pool/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorpsSansOrganes%2Fthread_pool/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33760645,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-01T02:00:06.963Z","response_time":115,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp11","semaphore","thread-pool","waitable-queue"],"created_at":"2024-11-16T07:34:43.659Z","updated_at":"2026-06-01T04:31:40.900Z","avatar_url":"https://github.com/CorpsSansOrganes.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Thread Pool\n## Overview\nThis repository contains C++11-compatible implementations for:\n\n1. Thread Pool\n2. Semaphore \n3. Waitable Queue. \n\nMy goal was to make a robust, reliable thread pool while keeping it beginner-friendly and easy to understand.\nEach component is well-documented and designed to make it as easy to use as possible.\nEach component is accompanied by a test file which, in addition to ensuring correctness, provide multiple extended examples for how to use these components.\n\nHere I'll provide an overview of each component, a technical documentation can be found inside each respective header file.\n\n## Build\nTo get this repository on your machine:\n```SHELL\ngit clone https://github.com/CorpsSansOrganes/thread_pool\n```\nIf you're using Linux, the makefile provided can be used to create executables for each module's respective test.\nSimply run in your shell:\n```SHELL\nmake all                  # Create all modules in release mode.\nmake mode=debug all       # Create all modules in debug mode.\n```\n\nYou can also create a specific module:\n```SHELL\nmake waitable_queue       # Create waitable_queue in release mode.\nmake mode=debug semaphore # Create semaphore in release mode.\n```\n\n## Table of Content\n  * [1. Thread Pool](#1-thread-pool)\n    + [What is it?](#what-is-it-)\n    + [Features](#features)\n    + [Usage](#usage)\n  * [2. Semaphore](#2-semaphore)\n    + [What is it?](#what-is-it--1)\n    + [Features](#features-1)\n    + [Usage](#usage-1)\n  * [3. Waitable Queue](#3-waitable-queue)\n    + [What is it?](#what-is-it--2)\n    + [Features](#features-2)\n    + [Usage](#usage-2)\n    \n## 1. Thread Pool \n### What is it?\nA **thread pool** is an object which offers an API for distributing tasks among a group of threads, which then execute the submitted tasks\nconcurrently.\n\nBy having a dedicated object for multithreaded operations, a thread pool reduces the overhead cost of having to create and destroy threads.\nAdditionally, it alleviates the headache of managing synchronization between threads.\n\n### Features\nThis particular implementations has the following key features:\n1. Any callable (function, functor, lambda...) can be submitted as a task, alongside any number of arguments.\n2. Tasks can return values asynchronously.\n3. Add or remove threads from the thread pool at runtime.\n4. Pause and resume the thread pool.\n\n### Usage\n```C++\n// Create a new thread pool with maximum threads supported by the hardware.\nThreadPool tp; \n\n// Submit task with arguments.\nauto sum_task = [](int x, int y) { return x + y; };\nauto result = tp.Submit(sum_task, 2, 2);\n\n// Get result back.\nassert(result.get() == 4);\n```\n\nAdditional examples can be found under `BasicUsageTest()` at `test/thread_pool_test.cpp`.\n\n## 2. Semaphore\n### What is it?\nA **semaphore** is a signalling device, used for synchronization between threads.\nEach semaphore has a counter. When a thread reaches a semaphore it checks its counter. If the counter is positive, the counter is decremented\nand the thread passes. Otherwise, if the counter is zero, the thread will be blocked until the semaphore is incremented by another thread.\n\n### Features\nPortable, c++11 compatible implementation.\n\n### Usage \n```C++\n// Create a new semaphore with an initial counter 0.\nSemaphore sem;\n\n// Create a thread which will be blocked on the semaphore.\nstd::thread t([\u0026] {\n  sem.Acquire();\n  std::cout \u003c\u003c \"Thread 2\" \u003c\u003c std::endl;\n}\n\n// Print \u0026 then unblock the thread.\nstd::cout \u003c\u003c \"Thread 1\" \u003c\u003c std::endl;\nsem.Release();\n```\n\nAdditional examples can be found under `test/semaphore_test.cpp`.\n\n## 3. Waitable Queue\n### What is it?\nA **waitable queue** is a thread-safe data structure based on a queue. It allows for multiple threads to insert new items, remove existing\nitems or wait until a new item becomes available.\n\n### Features \n1. Flexible: any container supporting push(), pop(), and front() to be used as the underlying data structure.\n2. Lightweight: simply include `waitable_queue.hpp` to use it.\n\n### Usage\n```C++\n// Create a new empty waitable queue.\nWaitableQueue\u003cint\u003e waitable_queue;\nstd::mutex mutex;\nint sum = 0;\n\n// Create 3 threads, consuming values from the queue and summing them.\nfor (int i = 0; i \u003c 3; ++i) {\n  std::thread t([\u0026] {\n    while (true) {\n      int value = waitable_queue.Dequeue();\n      {\n        std::unique_lock\u003cstd::mutex\u003e lock(mutex);\n        sum += value;\n      }\n    }\n  });\n}\n\n// Produce values from main thread\nfor (int i = 0; i = 100; ++i) {\n  waitable_queue.Enqueue(i);\n}\n\n// Sleep to let consumers finish, sum should be 4950 (= 100 * 99 / 2).\nstd::this_thread::sleep_for(std::chrono::seconds(1));\nassert(sum == 4950);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorpssansorganes%2Fthread_pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcorpssansorganes%2Fthread_pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorpssansorganes%2Fthread_pool/lists"}