This repository houses a C++ implementation of a Task Scheduler, leveraging threads to facilitate concurrent task execution. It provides a foundational framework for scheduling and executing tasks in a multi-threaded environment, demonstrating the effective use of threads, mutexes, and condition variables in C++.
- Task Creation and Execution: Define tasks with specific functionalities that can be executed concurrently.
- Dynamic Task Scheduling: Tasks are queued and scheduled dynamically across multiple threads for execution.
- Thread Pool Management: Utilizes a pool of threads to execute tasks concurrently, optimizing resource use and execution time.
- Concurrency Control: Ensures thread safety through mutexes and condition variables, managing access to the task queue.
- A modern C++ compiler that supports C++11 (or later).
- Basic understanding of C++ threading and object-oriented programming.
task.hpp/cpp: Defines theTaskclass for task creation and management.TaskScheduler.hpp/cpp: Implements the Task Scheduler, managing the task queue and thread pool.main.cpp: Demonstrates the usage of the Task Scheduler with example tasks.compile.sh: A shell script to compile the project usingg++and link the necessary files.
-
Compile the Project:
Use the provided
compile.shscript to compile the project. This script compilesmain.cpp,task.cpp, andTaskScheduler.cppwith the-lpthreadflag to enable POSIX threading support../compile.sh
-
Run the Executable:
After compilation, run the generated executable to start the task scheduler and execute the tasks:
./Executable
- Ensure that
task.hpp/cpp,TaskScheduler.hpp/cpp, and yourmain.cppare in the same directory. TheTaskSchedulerrelies on the functionality provided bytask.hpp/cppfor task management. - The
Taskclass is designed to be extended for specific tasks. Each task must implement theexecute()method, defining the task's behavior. - The
TaskSchedulerclass manages a pool of worker threads that execute tasks from a queue. It demonstrates basic concurrency control mechanisms, such as mutexes and condition variables, to safely manage access to the shared task queue.
To use the Task Scheduler in your project, define your tasks by extending the Task class and implementing the execute() method. Add tasks to the scheduler using addTask(std::unique_ptr<Task> task). The scheduler automatically handles the execution of tasks across its thread pool.
Contributions to improve the Task Scheduler or extend its functionality are welcome. Please feel free to fork the repository, make changes, and submit a pull request.