This project implements a basic user-level thread library in C, featuring cooperative multitasking and synchronization primitives. It provides an educational comparison between custom threads and POSIX pthreads
using workloads like matrix multiplication and producer-consumer problems.
- Custom thread creation, scheduling, and joining
- Cooperative multitasking via
mythread_yield
- Spinlock implementation for thread synchronization
- Matrix multiplication with performance benchmarking
- Producer-consumer problem with thread safety
- Performance comparison with
pthread
library using Gnuplot
userthread.c
: Core thread library implementationuserthread.h
: Thread library headermain.c
: Entrypoint for testing APIsmatrix.c
: Matrix multiplication using custom threadspthreadmatrix.c
: Matrix multiplication using pthreadsproducer_consumer.c
: Producer-consumer implementationplotmatrix.c
: Plotting code for benchmarkingplot.gnu
: Gnuplot script to visualize resultsmythreaddata.txt
: Custom thread timing datapthreaddata.txt
: Pthread timing datatest1.c
totest5.c
: Unit and concurrency testsMakefile
: Build scriptREADME.md
: Project documentation
To build everything:
make all
This compiles the thread library and all binaries. If directories for binaries, libraries, or logs are needed, create them:
mkdir -p bin lib log
Using custom threads:
bin/matrix <threads> <matrix_size>
Using pthreads:
bin/pthreadmatrix <threads> <matrix_size>
bin/producer_consumer
make run
To compare custom thread vs. pthread performance:
gnuplot plot.gnu
This uses mythreaddata.txt
and pthreaddata.txt
for plotting.
Target | Description |
---|---|
make all |
Build everything |
make run |
Run all test programs |
make clean |
Clean compiled files |
matrix |
Run matrix multiplication |
producer_consumer |
Run producer-consumer problem |
- Ensure
mythread_gettid
is correctly defined inuserthread.c
and declared inuserthread.h
. - Use
-std=gnu99
or later if inline assembly issues occur. - For errors like
MAP_STACK
or__jmpbuf
, comment out system-dependent code for portability.
MIT License — free to use, modify, and share.
Developed by Gaurav Chaudhari
M.Tech, Computer Science — IIT Madras
For questions or suggestions, raise an issue or submit a PR!