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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
import sync.atomic2
import sync
const (
iterations_per_cycle = 100_000
)
struct Counter {
mut:
counter u64
}
// without proper syncronization this would fail
fn test_count_10_times_1_cycle_should_result_10_cycles_with_sync() {
desired_iterations := 10 * iterations_per_cycle
mut wg := sync.new_waitgroup()
mut counter := &Counter{}
wg.add(10)
for i := 0; i < 10; i++ {
go count_one_cycle(mut counter, mut wg)
}
wg.wait()
assert counter.counter == desired_iterations
eprintln(' with synchronization the counter is: ${counter.counter:10} , expectedly == ${desired_iterations:10}')
}
// This test just to make sure that we have an anti-test to prove it works
fn test_count_10_times_1_cycle_should_not_be_10_cycles_without_sync() {
desired_iterations := 10 * iterations_per_cycle
mut wg := sync.new_waitgroup()
mut counter := &Counter{}
wg.add(10)
for i := 0; i < 10; i++ {
go count_one_cycle_without_sync(mut counter, mut wg)
}
wg.wait()
// NB: we do not assert here, just print, because sometimes by chance counter.counter may be == desired_iterations
eprintln('without synchronization the counter is: ${counter.counter:10} , expectedly != ${desired_iterations:10}')
}
fn test_count_plus_one_u64() {
mut c := u64(0)
atomic2.add_u64(&c, 1)
assert atomic2.load_u64(&c) == 1
}
fn test_count_plus_one_i64() {
mut c := i64(0)
atomic2.add_i64(&c, 1)
assert atomic2.load_i64(&c) == 1
}
fn test_count_plus_greater_than_one_u64() {
mut c := u64(0)
atomic2.add_u64(&c, 10)
assert atomic2.load_u64(&c) == 10
}
fn test_count_plus_greater_than_one_i64() {
mut c := i64(0)
atomic2.add_i64(&c, 10)
assert atomic2.load_i64(&c) == 10
}
fn test_count_minus_one_u64() {
mut c := u64(1)
atomic2.sub_u64(&c, 1)
assert atomic2.load_u64(&c) == 0
}
fn test_count_minus_one_i64() {
mut c := i64(0)
atomic2.sub_i64(&c, 1)
assert atomic2.load_i64(&c) == -1
}
fn test_count_minus_greater_than_one_u64() {
mut c := u64(0)
atomic2.store_u64(&c, 10)
atomic2.sub_u64(&c, 10)
assert atomic2.load_u64(&c) == 0
}
fn test_count_minus_greater_than_one_i64() {
mut c := i64(0)
atomic2.store_i64(&c, 10)
atomic2.sub_i64(&c, 20)
assert atomic2.load_i64(&c) == -10
}
// count_one_cycle counts the common counter iterations_per_cycle times in thread-safe way
fn count_one_cycle(mut counter Counter, mut group sync.WaitGroup) {
for i := 0; i < iterations_per_cycle; i++ {
atomic2.add_u64(&counter.counter, 1)
}
group.done()
}
// count_one_cycle_without_sync counts the common counter iterations_per_cycle times in none thread-safe way
fn count_one_cycle_without_sync(mut counter Counter, mut group sync.WaitGroup) {
for i := 0; i < iterations_per_cycle; i++ {
counter.counter++
}
group.done()
}
|