Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

SimpleMultithreader

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.


What It Does

The core idea: instead of writing raw pthread_create / pthread_join boilerplate everywhere, you pass a lambda and a range, and the library:

  1. Splits the loop range evenly across a chosen number of threads
  2. Spawns pthreads, each running the lambda over its assigned chunk
  3. Joins all threads and reports the total execution time

It supports both:

  • 1D parallelismparallel_for(low, high, lambda, numThreads)
  • 2D parallelismparallel_for(low1, high1, low2, high2, lambda, numThreads)

Demo programs

  • matrix.cpp — parallel matrix multiplication using the 2D interface
  • vector.cpp — parallel vector operations using the 1D interface

Tech & Concepts

  • 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

Getting Started

Prerequisites

  • A C++ compiler with C++11 support (GCC/g++)
  • make
  • A Linux/Unix environment (for pthreads)

Build & Run

# 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

Project Structure

.
├── 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

How It Works (High Level)

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.


What I Learned

  • 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::function and wrapper structs
  • Measuring and reasoning about parallel vs. serial performance
  • [Add your own line — e.g. handling uneven work splits, avoiding race conditions]

Future Improvements

  • Thread pooling to avoid repeated thread creation overhead
  • Dynamic load balancing for uneven workloads
  • Benchmark suite comparing speedup across thread counts

About

Multithreader

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages