RTOS multiple threads not working

29 Sep 2015

Due to frustration I reduced my code down to two threads on the RTOS, each meant to turn on indivudual LEDs. However, it seems that the LPC11u24 only runs the thread which is declared first, for example the code order:

     Thread thread3(threadA);
     Thread thread2(threadB);

would result in threadA running but threadB not running (debugging using LEDs)

but

     Thread thread3(threadB);
     Thread thread2(threadA);

would result in threadB running and threadA not running!!!

Note that the "main" thread the while loop runs OK no matter what... I have tried adding in wait() statements, but to no avail, anyone else had this problem? Anyone know how to fix it? Looks like a bug to me... Thanks for the help...

full code

#include "mbed.h"
#include "rtos.h"


DigitalOut led_a(P1_23);
DigitalOut led_b(P1_28);
DigitalOut led_c(P0_16);


        
        

         void threadA(void const *args) 
         {
            while (true) 
            { 
               led_b=1;
             }
        }
 
         void threadB(void const *args) 
         {
            while (true) 
            {  
                led_a=1;

             }
            
        }

        
        
int main()
{
    
    // INITIALISE RTOS THREADS //
     Thread thread3(threadA);
     Thread thread2(threadB);
     // END INITIALISE RTOS THREADS //
       
    while(1) 
    {
                led_c=1; 
    }
    }
29 Sep 2015

Since they are equal priority, and one never releases its hold, you don't really know what will happen (I thought it would mix up equal priority threads, but guess not). If you add Thread::wait statements it should work properly (note: despite the name those waits are in ms). Compared to regular wait, that tells the RTOS that the thread is idle.

19 Apr 2016
  1. include "mbed.h"
  2. include "rtos.h" AnalogIn p1(p20); PwmOut buz(p21); DigitalOut led2(LED2); DigitalOut led4(LED4); float sen,n; float stepsize=3/4096; float vout,c; void temp_thread(void const *args) { while(1){

sen=p1.read(); vout= (sen/1024.0)*5000; c=vout/10;

Thread thread2(led2_thread); Thread::wait(1000); }

} void led2_thread(void const *args) { while (true) { if(c < 25.0) { led2 = 1; buz = 0; Thread::wait(1000);

} else { led4 = 1; buz = 1; Thread::wait(1000); } Thread::wait(1000); } }

int main() { Thread thread(temp_thread); Thread thread2(led2_thread);

while (true) { Thread::wait(5); } }

unable to generate buzzer according to change of temperature of lm35