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

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



































            FIG. 15.8  Example program without mutex.



              In Fig. 15.9 (program: MutexEx) the same program is given where a mutex is used to lock
            the shared printf resource. This modified program displays the following messages on the
            screen which is what is expected normally:
              Before 1
              After 1
              Before 2
              After 2



            15.6.2 Semaphores

              A semaphore is simply a nonnegative integer which increments and decrements and con-
            trolsaccesstoasharedresource.Asemaphoreisinitiallysettoacountequivalenttothenumber
            of free resources. A semaphore’s value must be positive to allow access to the shared resource.
            The semaphore count is decremented by one when a thread uses the semaphore. Similarly, the
            count is incremented by one when the thread releases the semaphore. A zero count does not
            allow access to the shared resource. Semaphores with only one count are similar to mutexes.
            Such semaphores are also known as Binary Semaphores. An analogy of semaphores is the us-
            ageofaprinter(sharedresource),forexample,threeuserssendingprintrequestsallatthesame
   384   385   386   387   388   389   390   391   392   393   394