MPMT v1.7.4
MPMT

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()
{
Mutex mutex = createMutex();
if (!mutex)
abort();
lockMutex(mutex);
// Do some synchronized work...
unlockMutex(mutex);
destroyMutex(mutex);
}
// ========================================
static void onUpdate(void* arument)
{
volatile bool* isRunning = argument;
while (*isRunning)
{
// Do some parallel work...
sleepThread(0.001);
}
}
void threadExample()
{
volatile bool isRunning = true;
Thread thread = createThread(
onUpdate, &isRunning);
if (!thread)
abort();
isRunning = false;
joinThread(thread);
destroyThread(thread);
}
T abort(T... args)
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
  • macOS
  • Ubuntu (Linux)

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 CI

  • Windows: ./scripts/build-release.bat
  • macOS / Ubuntu: ./scripts/build-release.sh

Usage

Thread example: examples/thread_example.c
Mutex example: examples/mutex_example.c