Scheduling error while pending on getchar ?

03 Apr 2018

Hello, I'm trying to use Mbed OS on a Nucleo F446RE. I need 3 threads :

  • A high priority thread, that do a lot of computation
  • A middle priority thread, that read commands on the serial port using getchar & scanf.
  • A low priority thread that do low priority stuff...

In this example, high priority thread is running normaly, then background thread is launched, then finally middle priority thread is launched, call getchar and is blocked ( until I type something on the serial line...)

The problem is that when the middle priority thread is blocked on getchar, the background thread should be sheduled... and is not :-/

So... normal behavior ? Bug ?

Implementation example

#include <cinttypes>
#include "mbed.h"


EventQueue highQueue;
Thread highThread(osPriorityRealtime);

EventQueue middleQueue;
Thread middleThread(osPriorityHigh);

EventQueue lowQueue;
Thread lowThread(osPriorityLow);

Serial pc(USBTX, USBRX);

void high_prio()
{
	printf("High\n");
	//Do high priority computation...
	rtos::Thread::yield();
}

void middle_prio()
{
	while(1)
	{
		int32_t consigneValue;
		char c = getchar();
		printf("C=%c\n", c);

		switch (c)
		{
			case 'h':
                scanf("%" PRIi32, &consigneValue);
                // ....
				break;

			// [..]
		}
	}
}

void background()
{
	while(1)
	{
		printf("back\n");
	}
}

int main()
{
    pc.baud(115200);

    highThread.start(callback(&highQueue, &EventQueue::dispatch_forever));
    highQueue.call_every(10, high_prio);

    middleThread.start(callback(&middleQueue, &EventQueue::dispatch_forever));
    middleQueue.call_in(300, middle_prio);

    lowThread.start(callback(&lowQueue, &EventQueue::dispatch_forever));
    lowQueue.call_in(200, background);

    wait(osWaitForever);
}
17 Aug 2018

Hello,

I kinda have the same problem as you describe. Have you figured out how to solve this problem?