-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathatomic_singleton.c
57 lines (52 loc) · 1.57 KB
/
atomic_singleton.c
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
static atomic_bool lock = false;
static int *int_ptr = NULL;
ATOMIC_HACK_DECLARE
void* thread_start(void *arg)
{
// int_ptr does not have to be atomic, as its visibility
// is guarded by the atomic lock.
while (int_ptr == NULL)
{
bool expected_bool = false;
// On compare exchange success, memory_order_acquire
// ensures no memory read or write can be reordered
// before this point.
//
// On compare exchange failure, no memory reordering
// is depending on it so we can use memory_order_relazed.
if (!atomic_compare_exchange_weak_explicit(
&lock, &expected_bool, true,
memory_order_acquire,
memory_order_relaxed))
{
continue;
}
// now we can perform side effect to get singleton
int_ptr = malloc(sizeof(int));
*int_ptr = 5;
// 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);
}
printf("singleton int_ptr addr: %p, value: %d\n", int_ptr, *int_ptr);
return NULL;
}
int main()
{
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);
printf("singleton int_ptr addr: %p, value: %d\n", int_ptr, *int_ptr);
return 0;
}