Strange issue with thread priorities...
Posted: Mon Feb 06, 2012 8:38 am
Hi,
I'm seeing a very weird problem related to the priorities of the threads. I tried to simplify the setup that I have, and here is what I got:
The whole thing basically consists of three threads plus the main thread. The main thread starts a master thread which then starts two child threads. Now I want the child threads to have priority of master thread -1, I know that priorities are relative so i hope it's safe to expect that changing the priorities THREADA_PRIO, THREADB_PRIO and MASTERTHREAD_PRIO that I have in the code above by a constant shouldn't really change the behavior of the system.
Now the problem is that if I set these priorities as in the code, the system works as expected. However if I add say 20 to each of these priorities (preserving the differences) and so making them all above NORMALPRIO, the system doesn't work - only the main while loop works, none of the other three threads gets started.
I can try to debug to see what's wrong, I just want to make sure I'm not missing anything... To me this code looks ok, I don't see why this should be happening, seems like there is a bug in the code that creates/starts a new thread that has a higher priority than the creator itself.... I may be wrong though.
I running this on STM32F4Discovery and using trunk r3925.
I'm seeing a very weird problem related to the priorities of the threads. I tried to simplify the setup that I have, and here is what I got:
Code: Select all
#include "ch.hpp"
#include "hal.h"
#include "test.h"
using namespace chibios_rt;
#define THREADA_PRIO NORMALPRIO-10
#define THREADB_PRIO NORMALPRIO-10
#define MASTERTHREAD_PRIO NORMALPRIO-9
class ThreadA: public EnhancedThread<256>
{
public:
ThreadA() : EnhancedThread<256>("ThreadA", THREADA_PRIO)
{ }
protected:
virtual msg_t Main(void)
{
while (true) {
chThdSleepMilliseconds(50);
}
return 0;
}
};
class ThreadB: public EnhancedThread<256>
{
public:
ThreadB() : EnhancedThread<256>("ThreadB", THREADB_PRIO)
{ }
protected:
virtual msg_t Main(void)
{
while (true) {
chThdSleepMilliseconds(50);
}
return 0;
}
};
class MasterThread : public EnhancedThread<2048>
{
public:
MasterThread()
: EnhancedThread<2048>("MasterThread", MASTERTHREAD_PRIO)
{ }
protected:
virtual msg_t Main(void)
{
ThreadA ta;
ThreadB tb;
while (true) {
chThdSleepMilliseconds(50);
}
return 0;
}
};
int main(void)
{
halInit();
System::Init();
MasterThread mthread;
while (true) {
chThdSleepMilliseconds(100);
}
return 0;
}The whole thing basically consists of three threads plus the main thread. The main thread starts a master thread which then starts two child threads. Now I want the child threads to have priority of master thread -1, I know that priorities are relative so i hope it's safe to expect that changing the priorities THREADA_PRIO, THREADB_PRIO and MASTERTHREAD_PRIO that I have in the code above by a constant shouldn't really change the behavior of the system.
Now the problem is that if I set these priorities as in the code, the system works as expected. However if I add say 20 to each of these priorities (preserving the differences) and so making them all above NORMALPRIO, the system doesn't work - only the main while loop works, none of the other three threads gets started.
I can try to debug to see what's wrong, I just want to make sure I'm not missing anything... To me this code looks ok, I don't see why this should be happening, seems like there is a bug in the code that creates/starts a new thread that has a higher priority than the creator itself.... I may be wrong though.
I running this on STM32F4Discovery and using trunk r3925.