A small library providing generic interface for multithreading across different platforms.
Created due to the fact that macOS does not support <threads.h>
in C11.
See the documentation.
Features
- Mutex (Mutual exclusion)
- Cond (Condition variable)
- Thread (sleep, yield, etc.)
- Thread pool (tasks)
- Atomics (fetch add)
Usage example
void mutexExample()
{
if (!mutex)
abort();
}
static void onUpdate(void* arument)
{
volatile bool* isRunning = argument;
while (*isRunning)
{
}
}
void threadExample()
{
volatile bool isRunning = true;
onUpdate, &isRunning);
if (!thread)
isRunning = false;
}
Mutex createMutex()
Create a new mutex instance.
void lockMutex(Mutex mutex)
Locks the mutex, blocks if the mutex is not available.
void unlockMutex(Mutex mutex)
Unlocks locked mutex.
Mutex_T * Mutex
Mutual exclusion instance.
Definition sync.h:34
void destroyMutex(Mutex mutex)
Destroys mutex instance.
void joinThread(Thread thread)
Blocks the current thread until the function execution end.
void destroyThread(Thread thread)
Destroys thread instance.
void sleepThread(double delay)
Blocks the execution of the current thread for a specified time.
Thread_T * Thread
Thread instance.
Definition thread.h:37
Thread createThread(void(*function)(void *), void *argument)
Creates a new thread executing the specified function.
Supported operating systems
- Windows (10/11)
- Ubuntu (22.04/24.04)
- macOS (14/15)
This list includes only those systems on which functionality testing is conducted. However, you can also compile it under any other Linux distribution or operating system.
Build requirements
Use building instructions to install all required tools and libraries.
CMake options
Name | Description | Default value |
MPMT_BUILD_SHARED | Build MPMT shared library | ON |
MPMT_BUILD_TESTS | Build MPMT library tests | ON |
MPMT_BUILD_EXAMPLES | Build MPMT usage examples | ON |
CMake targets
Name | Description | Windows | macOS | Linux |
mpmt-static | Static MPMT library | .lib | .a | .a |
mpmt-shared | Dynamic MPMT library | .dll | .dylib | .so |
Cloning
git clone https://github.com/cfnptr/mpmt
Building 
- Windows:
./scripts/build-release.bat
- macOS / Ubuntu:
./scripts/build-release.sh
Usage
Thread example: examples/thread_example.c
Mutex example: examples/mutex_example.c