Basic example showing the Semaphore API

Dependencies:   mbed-rtos mbed

mbed 2 and mbed OS 5

This is an mbed 2 example. mbed OS 5 has integrated the mbed library with mbed-rtos. For an mbed-os example, please see:

Import programrtos_semaphore

semaphore example

Committer:
mbed_official
Date:
Wed Feb 15 13:54:49 2017 -0600
Revision:
6:1a383b363dc3
Parent:
5:574f47121e8e
Revert update to mbed-os.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:bdf73f017a77 1 #include "mbed.h"
mbed_official 6:1a383b363dc3 2 #include "rtos.h"
emilmont 1:bdf73f017a77 3
emilmont 1:bdf73f017a77 4 Semaphore two_slots(2);
mbed_official 6:1a383b363dc3 5
emilmont 1:bdf73f017a77 6 void test_thread(void const *name) {
emilmont 1:bdf73f017a77 7 while (true) {
emilmont 1:bdf73f017a77 8 two_slots.wait();
emilmont 1:bdf73f017a77 9 printf("%s\n\r", (const char*)name);
mbed_official 6:1a383b363dc3 10 Thread::wait(1000);
emilmont 1:bdf73f017a77 11 two_slots.release();
emilmont 1:bdf73f017a77 12 }
emilmont 1:bdf73f017a77 13 }
emilmont 1:bdf73f017a77 14
emilmont 1:bdf73f017a77 15 int main (void) {
mbed_official 6:1a383b363dc3 16 Thread t2;
mbed_official 6:1a383b363dc3 17 Thread t3;
mbed_official 6:1a383b363dc3 18
Bartek Szatkowski 4:ec4791bc6554 19 t2.start(callback(test_thread, (void *)"Th 2"));
Bartek Szatkowski 4:ec4791bc6554 20 t3.start(callback(test_thread, (void *)"Th 3"));
Bartek Szatkowski 4:ec4791bc6554 21
emilmont 1:bdf73f017a77 22 test_thread((void *)"Th 1");
emilmont 1:bdf73f017a77 23 }