question about thread priority

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
yushanh
Posts: 31
Joined: Mon Jul 29, 2013 6:23 pm

question about thread priority

Post by yushanh »

Hi all,

I'm new to multithread programming and would like to ask some basic questions while using ChibiOS.

I need to design my software like this: Thread A is running initially, but before it finishes the process, an interrupt comes in and wake up Thread B . However, I hope to let Thread A finish its process first before Thread B starts running. In this case, what kind of feature in ChibiOS shall I use? Would it be achieved if I give Thread A a higher priority? Or the mutex feature?

Another occasion is that, what if the interrupt comes in twice while Thread A is still running, would it wake up Thread B twice as well? What could happen to the shared resource?

Hope someone could help.

Thanks a lot,
Shan
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: question about thread priority

Post by Giovanni »

Hi,

The general rule is that the *ready* process with highest priority is the one being executed, this is true in any moment.

You just have to assign threads correct relative priorities.

Giovanni
yushanh
Posts: 31
Joined: Mon Jul 29, 2013 6:23 pm

Re: question about thread priority

Post by yushanh »

I see. Thanks Giovanni.

Originally I mix it up with the thread concept, which would share the CPU resource for both threads (the higher the more), in Linux OS. But seems like it defines the preemption priority.

I wonder if you could also help me with the second question? Really appreciated. :) When 2 interrupts comes in while thread A is still running, I assume the 2 thread B processes with lower priority would be queued before thread A finishes? If the second interrupt comes in while the thread B is running, would the original thread B be preempted by the second one? (they should be of the same priority)

Thanks,
Shan
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: question about thread priority

Post by Giovanni »

Threads at same priority are executed in FIFO order, an higher priority always preempts the currently executing one.
In RTOSes the concept of priority is VERY rigid, there is no time partitioning nor dynamic priorities.

You cannot wakeup a thread twice, the first time you make it ready, the second time it is already ready. Depending on the mechanism you use in order to wakeup the thread you may have side effects, for example a semaphore counter incremented twice.

Giovanni
Post Reply