Page 384 - ARM Based Microcontroller Projects Using MBED
P. 384

370                             15. Mbed RTOS PROJECTS

                 TABLE 15.1 Important Thread Functions
                 Function              Description                       Example
                 join()                Wait for a thread to terminate    thread1.join()
                 terminate()           Terminate a thread                thread1.terminate()
                 set_priority()        Set thread priority               thread1.set_priority(osPriorityHigh)
                 get_priority()        Get thread priority               p¼thread1.get_priority()
                 wait()                Wait specified milliseconds       Thread::wait(100)
                 gettid()              Get thread id of current thread   t¼Thread::gettid()

                   READY: These are the threads which are ready to run. When the current thread
                   relinquishes the CPU the next thread with the highest priority will run.
                   WAITING: the threads that are waiting for some events are in this state.
                   INACTIVE: threads that are terminated or are not started are in this state. These threads do
                   not consume any CPU resources.

                   A WAITING thread can become READY or RUNNING when the event it is waiting for
                 becomes available. A RUNNING thread becomes WAITING if it waits for an event. A RUN-
                 NING thread becomes READY if a higher priority thread becomes RAEDY. A thread becomes
                 INACTIVE when it terminates.


                 15.4.8 Terminating Thread LEDAControl After 10 Flashes
                   Insomeapplicationswe maywanttoterminate anactive thread.Wecanforexamplemodify
                 the program given in Fig. 15.5 so that thread LEDAControl is terminated after 10s. The main
                 code for this new program is as follows the remainder of the program is same as in Fig. 15.5:

                      int main()
                      {
                            thread1.start(LEDAControl);     // Start LEDAControl
                            thread2.start(LEDBControl);     // Start LEDBControl
                            Thread::wait(10000);            // Wait 10 seconds
                            thread1.terminate();            // Terminate LEDAControl
                            while(1);
                      }

                 15.4.9 Thread Callback

                   There are applications where we may want to pass parameters to a thread when the thread
                 is started. This is done using the callback function. An example program (program:
                 ThreadCallback) is shown in Fig. 15.6 where the flashing rate of the two LEDs are passed
                 as arguments to the two functions where LEDA flashing rate is 250ms and LEDB flashing
                 rate is 500ms. Note here that the addresses of the arguments must me passed to the functions
                 and the functions use pointers to read the data.
   379   380   381   382   383   384   385   386   387   388   389