Closed
Description
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
Labels
No labels