Skip to content
/ mpmt Public

Multi-platform multi-threading library, C99 <threads.h> alternative (mutex, cond, thread, pool)

License

Notifications You must be signed in to change notification settings

cfnptr/mpmt

Repository files navigation

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);
}

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