Skip to content

mbed #974ecf7 mutex not working (tested on K64F) #1837

Closed
@marcuschangarm

Description

@marcuschangarm

I'm using a mutex to protect access to a variable in a different thread. In the example below id should only be readable after it has been assigned a thread id, however, on a K64F the printf outputs 0 instead of the actual ID.

#include "mbed.h"
#include "rtos.h"

osThreadId id = 0;
Mutex mutex;

void threadFunction(const void* args)
{
    id = Thread::gettid();
    mutex.unlock();
}

int main()
{
    mutex.lock();
    Thread thread(threadFunction);

    mutex.lock();
    printf("ID: %p\r\n", id);

    for(;;)
    {
        wait(1);
    }
}

The equivalent using semaphores does produce the correct result:

#include "mbed.h"
#include "rtos.h"

osThreadId id = 0;
Semaphore semaphore(1);

void threadFunction(const void* args)
{
    id = Thread::gettid();
    semaphore.release();
}

int main()
{
    semaphore.wait();
    Thread thread(threadFunction);

    semaphore.wait();
    printf("ID: %p\r\n", id);

    for(;;)
    {
        wait(1);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions