-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
54 lines (42 loc) · 1004 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Copyright (c) 2018-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
Mutex mutex;
ConditionVariable cond(mutex);
// These variables are protected by locking mutex
uint32_t counter = 0;
bool done = false;
void worker_thread()
{
mutex.lock();
do {
printf("Worker: Count %lu\r\n", counter);
// Wait for a condition to change
cond.wait();
} while (!done);
printf("Worker: Exiting\r\n");
mutex.unlock();
}
int main()
{
Thread thread;
thread.start(worker_thread);
for (int i = 0; i < 5; i++) {
mutex.lock();
// Change count and signal this
counter++;
printf("Main: Set count to %lu\r\n", counter);
cond.notify_all();
mutex.unlock();
ThisThread::sleep_for(1000);
}
mutex.lock();
// Change done and signal this
done = true;
printf("Main: Set done\r\n");
cond.notify_all();
mutex.unlock();
thread.join();
}