Note
The project is currently under migration to CMake
BThread is a lightweight user-level threading library written in C. It provides a simple and efficient cooperative threading model with support for common synchronization primitives including mutexes, semaphores, condition variables, barriers, and thread-safe queues.
The library runs on Linux, macOS, and Windows systems with x86_64 architecture. It uses setjmp/longjmp for context switching and is designed to be portable across POSIX-compliant systems.
Core API:
bthread_create(): Create a new threadbthread_join(): Wait for a thread to terminatebthread_yield(): Yield execution to another threadbthread_exit(): Terminate the calling threadbthread_sleep(): Sleep for specified millisecondsbthread_cancel(): Request thread cancellationbthread_testcancel(): Check for cancellation requests
Synchronization Primitives:
- tmutex - Mutual exclusion locks
- tsemaphore - Counting semaphores
- tcondition - Condition variables
- tbarrier - Thread barriers
For detailed API documentation, see the header files in the include/ directory.
Building the library:
make # Build default demo and shared library
make lib # Build only the shared library
make demos # Build all demo programs
make test # Build and run tests
make clean # Clean build artifactsThe shared library will be built in the lib/ directory:
- Linux: libbthread.so
- macOS: libbthread.dylib
- Windows: bthread.dll
To use the library in your project:
- Include the header: #include <bthread.h>
- Link against the shared library: -lbthread
- Add the include path: -I/path/to/libbthread/include
- Add the library path: -L/path/to/libbthread/lib
Examples:
- Producer-Consumer patterns
- Dining Philosophers problem
- Reader-Writer locks
- Sleeping Barber problem
Common issues:
-
Compilation errors:
- Ensure you have GCC installed and updated
- Check that all source files are present
- Verify include paths are correct
-
Linking errors:
- Make sure the library path is in your LD_LIBRARY_PATH (Linux) or DYLD_LIBRARY_PATH (macOS)
- Verify the shared library was built successfully
-
Runti me issues:
- Check that threads are properly synchronized
- Ensure thread_yield() is called regularly in cooperative scheduling
- Verify all mutexes/semaphores are properly initialized
For bugs and issues, please report them at the project repository.
License: MIT License
Authors:
- Luca Mazza (C) 2025
This software is provided "as is" without warranty of any kind. See the LICENSE file for complete terms and conditions.