Page 390 - ARM Based Microcontroller Projects Using MBED
P. 390
376 15. Mbed RTOS PROJECTS
FIG. 15.9 Program using a mutex.
time, if all the jobs are to start in parallel at the same time then one user’s output will overlap
withanotherandtheoutputswillmixup.Wecanprotectthisprocessusingsemaphoredsothat
the printer resource is blocked when one process is running and then unblocked when it is fin-
ished. This process is then repeated for each user so that the jobs do not overlap. Mbed wait()
and release() functions are used to lock and unlock a semaphore.
An example program (program: sema)using asemaphore is givenin Fig. 15.10.Inthis
programfourthreads arecreated wheretheydisplay themessages First, Second, Third,
and Fourth on the screen. By using a semaphore with a count of 2 we restrict access at any
time to the printf function so that only two messages are displayed. The other two mes-
sages are displayed after a delay of 2s, that is, the display is as follows:
First
Second
<2 seconds delay>
Third
Fourth
<2 seconds delay>
First
Second
……………….
……………….