Arm Mbed OS support forum

Thread priority

I create Thread from main Thread.
After start thread I have printf(“some message”);
If I set priority to osPriorityNormal1 or above no further execution of the program.

etc.
if priority osPriorityNormal I see “some message” in console, if priority above, console is empty.

More details.
I am creating a program to control a car engine. One of the tasks is to read the readings from the crankshaft rotation sensor. There is a gear on the crankshaft, there are 35 teeth and one gap. Due to the fact that the port is bouncing, I read the value 10 times, and between readings I stop for 250 microseconds (exactly micro). Everything works, the readings are very accurate.
Further, I get the position with an accuracy of a degree, that is, in the interval between the impulses of the “full” and “empty” teeth, some time passes. Based on the average tooth travel time, since it is only 5 degrees, I calculate 1/5 of the time and add +1 degree. This is how I achieve degree accuracy. Everything works here, everything is fine.
Further, I need to give impulses to the nozzles and candles when a certain degree is reached, with an accuracy of 1.
If I do this in the same stream, then reading the readings from the sensor starts to slow down and the readings become inaccurate. If I create two separate streams, then since the priority between the main stream and the one I created is uneven, the readings start to slow down even more. If I set the priority to normal, I still see braking. If I set the priority higher than normal, then the readings from the sensor start to read well, but the execution of the main one stops. That is, after setting the priority, only the thread with the sensor reading is executed.
I need a main thread with normal priority. A very high priority sensor read flow and a flow to track the degree and generate pulses to the spark plugs and injectors.
What am I doing wrong? What is the point of thread priorities if they just disable the execution of other threads?

Hello Anton,

The following links might help:

https://www.keil.com/pack/doc/CMSIS/RTOS/html/exampleRTX_Tutorial.html

You don’t have to install the tools and examples. Just read the referenced CMSIS_RTOS_Tutorial.pdf. Mbed API is a bit different but the RTOS has the same base.

https://os.mbed.com/docs/mbed-os/v6.8/program-setup/concepts.html

https://os.mbed.com/docs/mbed-os/v6.12/apis/scheduling-rtos-and-event-handling.html

I read it all. The question is, why only the one with the highest priority is executed when prioritizing threads? Is this an mbed bug?

You can find the answer is in the last link above:

In Mbed, the high priority thread will continue to run until it enters a waiting state or until a thread of equal or higher priority is ready to run rather than to be executed in a time slot equal to all threads.

It means, if the high priority thread doesn’t enter a waiting state (because it cant complete it’s task on time) threads with lower priority will never get the chance to run.

If you think your microcontroller is fast enough to complete all the tasks on time you can try to force the high priority thread to enter the waiting state for a specified period of time by calling the

ThisThread::sleep_for(Clock::duration_u32 period_of_time);

method at the end of the endless loop of the high priority task. Unless there is a thread of equal or higher priority ready to run this will give the lower priority threads (which are in the ready to run state) chance to run.