Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,34 @@
|
||||
// vtest build: !windows && (amd64 || arm64)
|
||||
import sync
|
||||
|
||||
struct One {
|
||||
pub mut:
|
||||
i int
|
||||
}
|
||||
|
||||
fn (mut o One) add(i int) {
|
||||
o.i = o.i + i
|
||||
}
|
||||
|
||||
fn run(mut once sync.Once, mut o One, c chan bool) {
|
||||
once.do(fn [mut o] () {
|
||||
o.add(5)
|
||||
})
|
||||
c <- true
|
||||
}
|
||||
|
||||
fn test_once() {
|
||||
mut o := &One{}
|
||||
mut once := sync.new_once()
|
||||
c := chan bool{}
|
||||
n := 10
|
||||
|
||||
// It is executed 10 times, but only once actually.
|
||||
for i := 0; i < n; i++ {
|
||||
spawn run(mut once, mut o, c)
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
<-c
|
||||
}
|
||||
assert o.i == 5
|
||||
}
|
||||
Reference in new issue
Block a user