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
|
#include <iostream>
#pragma pack(2)
struct ShardValue {
char a[57];
uint64_t b;
};
char a;
ShardValue var;
void* thread_worker(void* k) {
uint64_t* v = (uint64_t*)k;
while (1) {
var.b = *v;
}
}
void* thread_observer(void* k) {
uint64_t v;
while(1) {
v = var.b;
if (v == 0x1111111100000001 ||
v == 0x4444444400000001 ||
v == 0x6666666600000001 ||
v == 0x2222222200000001 ||
v == 0x3333333300000001 ||
v == 0x5555555500000001) {
} else {
printf("v : %p\n", v);
exit(0);
}
}
}
int main() {
pthread_t tid_1, tid_2, tid_3, tid_4, tid_5, tid_6;
uint64_t v1 = 0x1111111100000001;
uint64_t v2 = 0x2222222200000001;
uint64_t v3 = 0x3333333300000001;
uint64_t v4 = 0x4444444400000001;
uint64_t v5 = 0x5555555500000001;
uint64_t v6 = 0x6666666600000001;
pthread_create(&tid_1, NULL, thread_worker, &v1);
pthread_create(&tid_2, NULL, thread_worker, &v2);
pthread_create(&tid_3, NULL, thread_worker, &v3);
pthread_create(&tid_4, NULL, thread_worker, &v4);
pthread_create(&tid_5, NULL, thread_worker, &v5);
pthread_create(&tid_6, NULL, thread_worker, &v6);
pthread_t o1, o2, o3, o4, o5, o6;
pthread_create(&o5, NULL, thread_observer, NULL);
pthread_create(&o6, NULL, thread_observer, NULL);
pthread_create(&o1, NULL, thread_observer, NULL);
pthread_create(&o2, NULL, thread_observer, NULL);
pthread_create(&o3, NULL, thread_observer, NULL);
pthread_create(&o4, NULL, thread_observer, NULL);
pthread_join(tid_1, NULL);
pthread_join(tid_2, NULL);
pthread_join(tid_3, NULL);
pthread_join(tid_4, NULL);
pthread_join(tid_5, NULL);
pthread_join(tid_6, NULL);
}
|