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

15.6 Mbed TASK SYNCHRONIZATION—MUTEX, SEMAPHORE, AND SIGNALS  377






























            FIG. 15.10  Semaphore example.




            15.6.3 Signals
              Signals are also called event flags and they are used for thread synchronization. A thread
            can be forced to wait for a signal to be set by another thread before it can continue. Mbed
            function signal_wait() forces the issuing thread to wait until the specified signal is set. Func-
            tion signal_set() sets a signal. We can have up to 32 signal flags (or event flags) per thread.
            Fig. 15.11 shows an example where one task generates some data and another one reads this
            data. The reading task waits for an event flag to indicate that data are available before it con-
            tinues to read it.
              An example program (program: signal) using signals is shown in Fig. 15.12.Thispro-
            gram uses the on-board LED and the on-board button. A thread called Flash is created
            which waits for event flag 1 to be set by calling function signal_wait(0x1).Event flag 1
            is set when the button is pressed by calling function signal_set(0x1). After this point
            thread Flash becomes active and flashes the LED every second. Remember that you have
            to load the RTOS library to your program before it can be compiled. As we have seen in
            earlier projects, the on-board LED and button are named as LED1 and BUTTON1,
            respectively.
   386   387   388   389   390   391   392   393   394   395   396