-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.c
More file actions
58 lines (52 loc) · 1.38 KB
/
Copy pathmain.c
File metadata and controls
58 lines (52 loc) · 1.38 KB
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
55
56
57
58
//
// main.c
// testtso
//
// Created by Saagar Jha on 5/28/21.
//
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
atomic_uint barrier;
atomic_uint data[10000];
void *writer(void *unused) {
(void)unused;
while (true) {
for (size_t i = 0; i < sizeof(data) / sizeof(*data); ++i) {
atomic_fetch_add_explicit(data + i, 1, memory_order_relaxed);
// https://bugs.llvm.org/show_bug.cgi?id=50564
// atomic_signal_fence(memory_order_acq_rel);
}
atomic_fetch_add_explicit(&barrier, 1, memory_order_release);
}
return NULL;
}
void *reader(void *unused) {
(void)unused;
unsigned int count = 0;
while (true) {
for (size_t i = 0; i < sizeof(data) / sizeof(*data) - 1; ++i) {
unsigned int value2 = atomic_load_explicit(data + i + 1, memory_order_relaxed);
// https://bugs.llvm.org/show_bug.cgi?id=50564
// atomic_signal_fence(memory_order_acq_rel);
unsigned int value1 = atomic_load_explicit(data + i, memory_order_relaxed);
if (value1 < value2) {
exit(0);
}
}
while (count == atomic_load_explicit(&barrier, memory_order_acquire))
;
++count;
}
return NULL;
}
int main() {
pthread_t writer_thread;
pthread_t reader_thread;
pthread_create(&writer_thread, NULL, writer, NULL);
pthread_create(&reader_thread, NULL, reader, NULL);
pthread_join(reader_thread, NULL);
pthread_join(writer_thread, NULL);
}