Question about RIOT/tests/thread_msg


I am a new beginner to learn RIOT. I got some question when I run the “thread_msg” on board “SAMR21-xpro”.

For the thread1, thread2 and thread3, they have the same priority. so when the thread3 is running, how can scheduler switch to the thread1.

I saw these in the API: “In case of equal priorities, the threads are scheduled in a semi-cooperative fashion. That means that unless an interrupt happens, threads with the same priority will only switch due to voluntary or implicit context switches.” and “Some functions that unblock another thread, e.g. [msg_send()]( or [mutex_unlock()](, can cause a thread switch, if the target had a higher priority.”

but here, all the three threads have the same priority, so according the rule, even there is [msg_send()](, the thread3 should not be blocked and run forever, but it doesn’t.

Thanks for your time!

Regards, Jianwen OUYANG

Hi Jianwen,

thread_msg does a context switch between p3 and p1 because p1 does not have a message queue. The second message from p3 causes p3 to go into a blocked state and give up control otherwise the previous message will be lost.

if in p1 a msg_queue is defined:

void *thread1(void *arg) { (void) arg;

msg_t msg_queue[8]; msg_init_queue(msg_queue, 8);

puts(“THREAD 1 start\n”); …

then thread_msg example will run as documented until the message queue is full.

I hope this little explanation may help …