2023-05-29 02:35:34 +03:00
|
|
|
// Build with
|
2023-06-12 13:05:59 +03:00
|
|
|
// v -gc none -use-coroutines simple_coroutines.v
|
2023-05-29 02:35:34 +03:00
|
|
|
//
|
|
|
|
import coroutines
|
|
|
|
import time
|
2023-06-03 20:05:50 +03:00
|
|
|
import os
|
|
|
|
import net.http
|
2023-05-29 02:35:34 +03:00
|
|
|
|
|
|
|
fn foo(a int) {
|
|
|
|
for {
|
2023-06-07 17:48:27 +03:00
|
|
|
println('1hello from foo() a=${a}')
|
2023-06-08 01:51:40 +03:00
|
|
|
// C.printf(c'hello from foo() a=%d\n', a)
|
2023-05-29 02:35:34 +03:00
|
|
|
coroutines.sleep(1 * time.second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo2(a int) {
|
2023-06-03 20:05:50 +03:00
|
|
|
mut i := 0
|
2023-05-29 02:35:34 +03:00
|
|
|
for {
|
2023-06-07 17:48:27 +03:00
|
|
|
println('hello from foo2() a=${a}')
|
2023-06-08 01:51:40 +03:00
|
|
|
// C.printf(c'hello from foo2() a=%d\n', a)
|
2023-05-29 02:35:34 +03:00
|
|
|
coroutines.sleep(2 * time.second)
|
2023-06-03 20:05:50 +03:00
|
|
|
i++
|
2023-06-12 13:05:59 +03:00
|
|
|
resp := http.get('https://vlang.io/utc_now') or { panic(err) }
|
|
|
|
println(resp)
|
|
|
|
mut f := os.create('/tmp/FOO2_a${i}') or { panic(err) }
|
|
|
|
f.write_string(resp.body) or { panic(err) }
|
|
|
|
f.close()
|
2023-05-29 02:35:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo3(a int) {
|
|
|
|
for {
|
2023-06-08 01:51:40 +03:00
|
|
|
println('hello from foo3() a=${a}')
|
|
|
|
// C.printf(c'hello from foo3() a=%d\n', a)
|
2023-06-04 00:56:07 +03:00
|
|
|
coroutines.sleep(3 * time.second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo4(a int) {
|
|
|
|
for {
|
2023-06-07 17:48:27 +03:00
|
|
|
println('hello from foo4() a=${a}')
|
2023-06-08 01:51:40 +03:00
|
|
|
// C.printf(c'hello from foo4() a=%d\n', a)
|
2023-05-29 02:35:34 +03:00
|
|
|
coroutines.sleep(3 * time.second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
go foo(10)
|
|
|
|
go foo2(20)
|
|
|
|
go foo3(30)
|
2023-06-04 00:56:07 +03:00
|
|
|
go foo3(40)
|
2023-06-03 20:05:50 +03:00
|
|
|
$if is_coroutine ? {
|
|
|
|
println('IS COROUTINE=true')
|
|
|
|
} $else {
|
|
|
|
println('IS COROUTINE=false')
|
|
|
|
}
|
2023-05-29 02:35:34 +03:00
|
|
|
for {
|
|
|
|
println('hello from MAIN')
|
|
|
|
coroutines.sleep(1 * time.second)
|
|
|
|
}
|
|
|
|
println('done')
|
|
|
|
}
|