Skip to content

darcyg32/ConcurrentTaskScheduler-Cpp23

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Concurrent Task Scheduler (C++23)

C++23 std::jthread Platform

Modern C++ thread-pool scheduler that distributes work across a fixed set of std::jthread workers. The goal is to demonstrate a clear, dependency-free approach to concurrent task execution that is easy to study and embed in other projects.

Description

The scheduler exposes a minimal API. Construct it with the desired number of worker threads and call submit() with any callable. Pending tasks live in a mutex-protected queue, while workers block on a condition variable until work arrives or shutdown is requested. The design is compact enough for teaching and prototyping but still follows best practices for synchronization and RAII-based shutdown.

Motivation

  • Simplicity: Keep the API and implementation small enough to reason about at a glance.
  • Correctness: Rely solely on standard C++ synchronization primitives.
  • Safety: Ensure threads shut down cleanly without manual joins.
  • Modern C++: Highlight how std::jthread, atomics, and condition variables fit together.

Architecture

Core Components

  1. Task queuestd::queue<std::function<void()>> guarded by a std::mutex.
  2. Worker thread pool – fixed number of std::jthread instances, each running the same worker loop.
  3. Synchronization primitives – mutex + std::condition_variable for coordination, std::atomic<bool> for the shutdown flag.

Thread Pool Construction

  • Reserve storage for all workers on construction.
  • Spawn num_threads workers immediately; each starts waiting on the condition variable for incoming tasks.

Task Submission

  • submit(F&&) acquires the queue mutex, pushes the callable via perfect forwarding, and notifies one worker.
  • Only a single worker wakes up, preventing unnecessary context switches.

Worker Loop

  1. Lock the queue using std::unique_lock.
  2. Wait until either a task is present or shutdown is signaled.
  3. If shutdown is true and the queue is empty, exit the loop.
  4. Otherwise, pop the next task, release the lock, and execute the callable.
  5. Repeat until shutdown completes.

Releasing the lock before running user code keeps other workers from blocking on long-running tasks.

Shutdown Handling

  • shutdown() sets the atomic flag while holding the mutex, then notify_all() to wake every worker.
  • Each worker drains remaining tasks before exiting.
  • The destructor calls shutdown() so the scheduler always terminates cleanly, and std::jthread handles joining automatically.

Key Features

  • Modern C++23 implementation (std::jthread, atomics, condition variables).
  • Thread-safe task queue with efficient worker wake-ups.
  • Template-based submit() accepts lambdas, function objects, or function pointers.
  • Graceful shutdown that drains outstanding work.
  • Zero external dependencies. Pure standard library.

Building and Running

Prerequisites

  • C++23-capable compiler (GCC 13+, Clang 17+, MSVC 17.6+).
  • POSIX threads support (on Linux/macOS). Windows builds rely on the MSVC threading runtime.

Compilation

g++ -std=c++23 -pthread src/*.cpp -o scheduler-demo

Execution

./scheduler-demo

The sample main spins up four worker threads, enqueues ten tasks, and pauses long enough for everything to finish.

Usage Example

#include "scheduler.hpp"
#include <iostream>
#include <thread>
#include <chrono>

int main() {
    Scheduler scheduler(4);

    for (int i = 0; i < 10; ++i) {
        scheduler.submit([i]() {
            std::cout << "Task " << i << " executed by thread\n";
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        });
    }

    std::this_thread::sleep_for(std::chrono::seconds(2));
}

Example Output

Task 0 executed by thread
Task 1 executed by thread
Task 2 executed by thread
Task 3 executed by thread
Task 4 executed by thread
...

Implementation Details

  • src/scheduler.hpp – Scheduler class definition and templated submit.
  • src/scheduler.cpp – Constructor, worker loop, shutdown logic.
  • src/main.cpp – Reference program illustrating setup, submission, and teardown.

About

A modern C++ thread-pool scheduler that distributes work across a fixed set of std::jthread workers.

Resources

Stars

Watchers

Forks

Languages