A lightweight multithreading utility written in C++ that parallelizes loops using POSIX Threads (pthreads) and C++11 lambda functions. It provides a simple interface to run 1D and 2D loop computations across multiple threads — without the caller having to manage thread creation, joining, or work distribution manually.
Built as part of my Operating Systems coursework to understand thread-level parallelism and how work is divided across CPU cores.
The core idea: instead of writing raw pthread_create / pthread_join boilerplate everywhere, you pass a lambda and a range, and the library:
- Splits the loop range evenly across a chosen number of threads
- Spawns pthreads, each running the lambda over its assigned chunk
- Joins all threads and reports the total execution time
It supports both:
- 1D parallelism —
parallel_for(low, high, lambda, numThreads) - 2D parallelism —
parallel_for(low1, high1, low2, high2, lambda, numThreads)
matrix.cpp— parallel matrix multiplication using the 2D interfacevector.cpp— parallel vector operations using the 1D interface
- Language: C++ (C++11)
- Core OS concepts: thread-level parallelism, work distribution, thread synchronization
- Threading: POSIX Threads (pthreads)
- C++ features: lambda functions,
std::function - Build: Makefile-based
- A C++ compiler with C++11 support (GCC/g++)
make- A Linux/Unix environment (for pthreads)
# Clone the repository
git clone https://github.com/Goyamjain06/OS_projecttttt.git
cd OS_projecttttt
# Build using the Makefile
make
# Run the matrix multiplication demo
./matrix
# Run the vector operations demo
./vector.
├── simple-multithreader.h # The core library: parallel_for implementations
├── matrix.cpp # Parallel matrix multiplication demo
├── vector.cpp # Parallel vector operations demo
├── Makefile # Build configuration
└── simple-multithreader.pdf # Assignment reference / problem statement
Each parallel_for call takes a range and a lambda. The range is divided into roughly equal chunks — one per thread. A small wrapper packages each chunk's bounds and the lambda into a struct, which is handed to pthread_create. Each thread executes the lambda over its slice of the range, and the main thread waits on pthread_join for all of them before continuing. Execution time is measured to compare against a serial baseline.
- How to divide computational work across threads for parallel speedup
- Using pthreads (
pthread_create,pthread_join) from C++ - Passing C++11 lambdas into thread functions via
std::functionand wrapper structs - Measuring and reasoning about parallel vs. serial performance
- [Add your own line — e.g. handling uneven work splits, avoiding race conditions]
- Thread pooling to avoid repeated thread creation overhead
- Dynamic load balancing for uneven workloads
- Benchmark suite comparing speedup across thread counts