-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathatomic.cx
More file actions
34 lines (27 loc) · 726 Bytes
/
Copy pathatomic.cx
File metadata and controls
34 lines (27 loc) · 726 Bytes
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
//! counter: 5\nflag: 1\natomic ptr: 99\n
include <stdio.h>
include <stdatomic.h>
include <stdint.h>
struct SharedState {
_Atomic u32 counter;
_Atomic bool ready;
}
void increment_counter(_Atomic u32* c, u32 times) {
for (u32 i = 0; i < times; i++) {
atomic_fetch_add(c, 1);
}
}
bool check_flag(_Atomic bool* flag) {
return atomic_load(flag);
}
int main() {
_Atomic u32 counter = 0;
increment_counter(&counter, 5);
printf("counter: %u\n", atomic_load(&counter));
_Atomic bool ready = true;
printf("flag: %d\n", check_flag(&ready));
_Atomic u32 value = 99;
_Atomic u32* atomic_ptr = &value;
printf("atomic ptr: %u\n", atomic_load(atomic_ptr));
return 0;
}