The effect is like this example written in vlang, which has a lock section to
prevent race on cursor
const STORIES_URL = 'https://hacker-news.firebaseio.com/v0/topstories.json'
const ITEM_URL_BASE = 'https://hacker-news.firebaseio.com/v0/item'
struct Story {
title string
}
fn main() {
resp := http.get(STORIES_URL)?
ids := json.decode([]int, resp.body)?
mut cursor := 0
for _ in 0..8 {
go fn() {
for {
lock {
if cursor >= ids.len {
break
}
id := ids[cursor]
cursor++
}
resp := http.get('$ITEM_URL_BASE/$id.json')?
story := json.decode(Story, resp.body)?
println(story.title)
}
}()
}
runtime.wait()
}
Run
- Lock code block without explicit lock variable ? jackhftang
- Re: Lock code block without explicit lock variable ? Araq
- Re: Lock code block without explicit lock variable ? jackhftang
- Re: Lock code block without explicit lock variable... jackhftang
