v2 / vlib / v / tests / concurrency / autolock_array1_test.v
48 lines · 45 sloc · 1.01 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1import time
2
3const iterations_per_thread = 100000
4
5fn add_elements(shared foo []int, n int) {
6 for _ in 0 .. iterations_per_thread {
7 foo << n
8 }
9 foo[0]++
10}
11
12fn test_autolocked_array() {
13 shared abc := &[0]
14 spawn add_elements(shared abc, 1)
15 spawn add_elements(shared abc, 3)
16 for _ in 0 .. iterations_per_thread {
17 abc << 0
18 }
19 // wait for coroutines to finish - that should really be
20 // done by channels, yield, semaphore...
21 for {
22 mut finished_threads := 0
23 rlock abc {
24 finished_threads = unsafe {
25 abc[0]
26 }
27 }
28 if finished_threads == 2 {
29 break
30 }
31 time.sleep(100 * time.millisecond)
32 }
33 // create histogram of results
34 mut result := [0, 0, 0, 0]
35 rlock abc {
36 // automatic rlock for iteration is also not implemented, yet
37 for v in abc {
38 if v > 3 {
39 panic('unexpected element on array')
40 }
41 result[v]++
42 }
43 }
44 assert result[0] == iterations_per_thread
45 assert result[1] == iterations_per_thread
46 assert result[2] == 1 // number of non-main threads
47 assert result[3] == iterations_per_thread
48}
49