sync: new_mutex() and new_waitgroup()

This commit is contained in:
Delyan Angelov 2019-10-25 17:24:40 +03:00 committed by Alexander Medvednikov
parent e04c4ad852
commit 32b3611026
4 changed files with 41 additions and 9 deletions

View file

@ -17,10 +17,10 @@ struct Story {
struct Fetcher {
mut:
mu sync.Mutex
mu &sync.Mutex
ids []int
cursor int
wg &sync.WaitGroup
wg &sync.WaitGroup
}
fn (f mut Fetcher) fetch() {
@ -65,12 +65,15 @@ fn main() {
ids = tmp
}
mut wg := &sync.WaitGroup{}
fetcher := &Fetcher{ids: ids, wg: wg} // wg sent via ptr
wg.add(ids.len)
wg := sync.new_waitgroup()
mtx := sync.new_mutex()
mut fetcher := &Fetcher{ids: ids}
fetcher.mu = &mtx
fetcher.wg = &wg
fetcher.wg.add(ids.len)
for i := 0; i < NR_THREADS; i++ {
go fetcher.fetch()
}
wg.wait()
fetcher.wg.wait()
}