-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathatomic_lock.c
More file actions
63 lines (58 loc) · 1.74 KB
/
atomic_lock.c
File metadata and controls
63 lines (58 loc) · 1.74 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
59
60
61
62
63
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stddef.h>
#include <stdio.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
static atomic_bool lock = false;
static int counter = 0;
bool global_false = true;
ATOMIC_HACK_DECLARE
void* thread_start(void *arg)
{
bool expected_bool = false;
for (int i = 0; i < 10000; i++)
{
// On compare exchange success, memory_order_acquire
// ensures no memory read or write can be reordered
// before this point.
//
// On compare exchange failure, expected_bool must
// be reload to the expected value (which is false).
// No memory reordering is depending on it so we can
// use memory_order_relazed
while (!atomic_compare_exchange_weak_explicit(
&lock, &expected_bool, true,
memory_order_acquire,
memory_order_relaxed))
{
expected_bool = false;
// gcc has a bug. It would ignore the statement above
// unless you perform this noop().
// asm("nop") doesn't work here.
// writing the same algorithm in c++11 has the same problem.
ATOMIC_HACK_OP;
}
// now we can perform side effect on counter
counter++;
// No memory read or write can be reordered after
// memory_order_release. The side effects are "visible"
// after memory_order_release.
atomic_store_explicit(&lock, false, memory_order_release);
}
return NULL;
}
int main()
{
global_false = false;
pthread_t p1, p2;
pthread_create(&p1, NULL, thread_start, NULL);
pthread_create(&p2, NULL, thread_start, NULL);
pthread_join(p1, NULL);
pthread_join(p2, NULL);
// should print 20000
printf("counter: %d\n", counter);
return 0;
}