1import sync 2 3const ( 4 iterations_per_thread2 = 100000 5) 6 7fn inc_elements(shared foo []int, n int, sem sync.Semaphore) { 8 for _ in 0 .. iterations_per_thread2 { 9 foo[n]++ 10 } 11 sem.post() // indicat that thread is finished 12} 13 14fn test_autolocked_array_2() { 15 shared abc := &[0, 0, 0] 16 sem := sync.new_semaphore() 17 go inc_elements(shared abc, 1, sem) 18 go inc_elements(shared abc, 2, sem) 19 for _ in 0 .. iterations_per_thread2 { 20 unsafe { 21 abc[2]++ 22 } 23 } 24 // wait for the 2 coroutines to finish using the semaphore 25 for _ in 0 .. 2 { 26 sem.wait() 27 } 28 rlock abc { 29 assert unsafe { abc[1] } == iterations_per_thread2 30 assert unsafe { abc[2] } == 2 * iterations_per_thread2 31 } 32} 33