Mistake on this page?
Report an issue in GitHub or email us

PlatformMutex

PlatformMutex class hierarchy

You can use the PlatformMutex class to synchronize the execution of threads.

The Mbed OS drivers use the PlatformMutex class instead of Mutex. This enables the use of drivers when the Mbed OS is compiled without the RTOS. For examples, please see AnalogIn, BusOut, SPI and I2C.

Note: For the standard use of RTOS mutexes, please see Mutex.

PlatformMutex class reference

Public Member Functions
 PlatformMutex ()
 Create a PlatformMutex object. More...
 ~PlatformMutex ()
 PlatformMutex destructor. More...
void lock ()
 Wait until a PlatformMutex becomes available. More...
void unlock ()
 Unlock a PlatformMutex that the same thread has previously locked. More...

PlatformMutex example

/*
 * Copyright (c) 2017 - 2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

PlatformMutex stdio_mutex;
Thread t2;
Thread t3;

void notify(const char *name, int state)
{
    stdio_mutex.lock();
    printf("%s: %d\n\r", name, state);
    stdio_mutex.unlock();
}

void test_thread(void const *args)
{
    while (true) {
        notify((const char *)args, 0);
        ThisThread::sleep_for(1000);
        notify((const char *)args, 1);
        ThisThread::sleep_for(1000);
    }
}

int main()
{
    t2.start(callback(test_thread, (void *)"Th 2"));
    t3.start(callback(test_thread, (void *)"Th 3"));

    test_thread((void *)"Th 1");
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.