![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Random Delay With Thread
Diff: main.cpp
- Revision:
- 0:be029d41beb4
diff -r 000000000000 -r be029d41beb4 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Jan 20 11:52:20 2017 +0000 @@ -0,0 +1,48 @@ +#include "mbed.h" +#include "rtos.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +void led1_thread(void const *args) { + while (true) { + led1 = !led1; + Thread::wait((rand()%700)+1); + } +} + +void led2_thread(void const *args) { + while (true) { + led2 = !led2; + Thread::wait((rand()%700)+1); + } +} + +void led3_thread(void const *args) { + while (true) { + led3 = !led3; + Thread::wait((rand()%700)+1); + } +} + +void led4_thread(void const *args) { + while (true) { + led4 = !led4; + Thread::wait((rand()%700)+1); + } +} + +int main() { + //Create a thread to execute the function led2_thread + Thread thread1(led1_thread); + Thread thread2(led2_thread); + Thread thread3(led3_thread); + Thread thread4(led4_thread); + + //led2_thread is executing concurrently with main at this point + + while (true) { + } +} \ No newline at end of file