-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
49 lines (40 loc) · 1007 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
/*
* Copyright (c) 2017 - 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#define USE_CRITICAL_SECTION_LOCK 1 // Set 0 to see race condition
// Note: Might require few runs to see race condition
#define THREAD_CNT 8
int32_t value = 100000;
volatile int32_t counter = 0;
void increment(void)
{
for (int i = 0; i < value; i++) {
#if (USE_CRITICAL_SECTION_LOCK == 1)
CriticalSectionLock lock;
#endif
counter += 1;
}
}
int get_count(void)
{
if (counter == (value * THREAD_CNT)) {
printf("No Race condition\n");
} else {
printf("Race condition\n");
}
return counter;
}
int main()
{
Thread counter_thread[THREAD_CNT];
for (int i = 0; i < THREAD_CNT; i++) {
counter_thread[i].start(callback(increment));
}
// Wait for the threads to finish
for (int i = 0; i < THREAD_CNT; i++) {
counter_thread[i].join();
}
printf("Counter = %d\n", get_count());
}