mirror of
https://github.com/vlang/v.git
synced 2025-09-14 15:02:33 +03:00
coroutines: use photon work_pool when nr_jobs > 0, and use photon libc fn wrappers (#19711)
This commit is contained in:
parent
57a7db11bf
commit
a63f3e6f77
7 changed files with 122 additions and 12 deletions
37
examples/coroutines/coroutines_bench.v
Normal file
37
examples/coroutines/coroutines_bench.v
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Build with (-gc none, until GC bug is fixed)
|
||||
// v -gc none -use-coroutines coroutine_benchs.v
|
||||
//
|
||||
import coroutines
|
||||
import time
|
||||
import net.http
|
||||
import sync
|
||||
|
||||
const run_time = 10 * time.second
|
||||
|
||||
fn request(mut mu sync.Mutex, count &int) {
|
||||
for {
|
||||
http.get('http://vlang.io/utc_now') or { panic(err) }
|
||||
mu.@lock()
|
||||
unsafe {
|
||||
(*count)++
|
||||
}
|
||||
mu.unlock()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut mu := sync.new_mutex()
|
||||
mut count := 0
|
||||
|
||||
for _ in 0 .. 8 {
|
||||
go request(mut mu, &count)
|
||||
}
|
||||
$if is_coroutine ? {
|
||||
println('IS COROUTINE=true')
|
||||
coroutines.sleep(run_time)
|
||||
} $else {
|
||||
println('IS COROUTINE=false')
|
||||
time.sleep(run_time)
|
||||
}
|
||||
println('${count} requests made.')
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue