Thread Yielding problem

I have the following simple problem. I am trying to auto initialize and run these threads. Even they can work one by one periodically. However, i am even if i am trying to switch to ccc thread from rcv thread using sched_run() or thread_yield(); it never runs the ccc thread. I tried to thread_sleep, but it is just another overhead and overload to run multiple threads sequentially.

Where am i wrong ? Thanks for your help.

#include “thread.h” #include “stdio.h” #include “msg.h” char rcv_thread_stack[THREAD_STACKSIZE_MAIN]; char ccc_thread_stack[THREAD_STACKSIZE_MAIN];

void *rcv_thread(void *arg) { thread_yield(); (void) arg; // msg_t m; while (1) {

printf("!!!"); -> // sched_run() or thread_yield();

} return NULL; } void ccc_thread(void arg) { (void) arg; // msg_t m; while (1) { printf("*****************"); } return NULL; } int main(void) { thread_create(rcv_thread_stack, sizeof(rcv_thread_stack), THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST, rcv_thread, NULL, “rcv_thread”);

thread_create(ccc_thread_stack, sizeof(ccc_thread_stack), THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST, ccc_thread, NULL, “ccc_thread”);

while(1){ printf("****"); } }


thread_create(rcv_thread_stack, sizeof(rcv_thread_stack), THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST, rcv_thread, NULL, "rcv_thread");

After the creating of this thread the main thread itself yields and the new thread is scheduled due to its higher priority (less means higher priority like with Unix nice values). When the new Thread yields it always get scheduled again, since there is no thread with an higher priority.

thread\_create\(ccc\_thread\_stack, sizeof\(ccc\_thread\_stack\),
              ccc\_thread, NULL, "ccc\_thread"\);

This thread is never created since the main Thread is not scheduled again.

You can use the option "THREAD_CREATE_WOUT_YIELD" to prevent the main thread from yielding. This would result in both threads are started and afterwards scheduled in an alternating way (assuming you yield in both threads), but would prevent you main loop from getting scheduled. If you need your main thread to run also, you can use the same priorities for all three threads.

Please not that the documentation states "Avoid assigning the same priority to two or more threads.". But i have no clue for what reason.



The reason is given right in the beginning of the documentation [1]. Sounds like the reasons is pretty much the behavior you try to achieve so it should be fine if you use thread_yield correctly.


Robin thanks for your suggestion i manage it to work! It works now. Thank you so much!

Robin <>, 25 Tem 2019 Per, 18:10 tarihinde şunu yazdı: