1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples: add a simple coroutine example

This commit is contained in:
Alexander Medvednikov 2023-05-29 01:35:34 +02:00
parent 1be539d20f
commit 3f62487409
3 changed files with 64 additions and 3 deletions

View File

@ -1,8 +1,27 @@
## [Version 0.3]
-- [x] gc option
-- [x] channels
-- [x] lock{}
-- [x] thread safe arrays
-- [x] rune type
-- [x] replace `ustring` with `[]rune`
-- [x] fix `byte.str()`
-- [x] maps with non-string keys
-- [x] iOS/Android support
-- [x] parallel cgen
-- [x] IO streams
-- [x] struct embedding
-- [x] interface embedding
-- [x] interfaces: allow struct fields (not just methods)
-- [x] short generics syntax (`foo(5)` instead of `foo<int>(5)`)
-- [x] more advanced errors, not just `error('message')`
## [Version 0.4]
- [x] [Coroutines](https://github.com/vlang/v/discussions/11582)
- [x] vfmt: add missing imports (like goimports)
- [ ] Recursive structs via options: `struct Node { next ?Node }`
- [x] Recursive structs via options: `struct Node { next ?Node }`
- [x] First class Option type
- [x] Optional function struct fields
- [ ] Handle function pointers safely, remove `if function == 0 {`
@ -13,6 +32,7 @@
- [x] New VPM site
## [Version 0.5]
- [ ] [Thread safe maps](https://github.com/vlang/v/discussions/11729)
- [ ] Parallel parser
- [ ] Parallel checker
@ -29,6 +49,7 @@
- [ ] Big remaining bugs fixed
- [ ] More powerful comptime
- [ ] Constraits for generics
- [ ] Coroutines on Windows
- [ ] Autofree memory management option ready for production
- [ ] C2V supporting entire C99 standard

View File

@ -0,0 +1,37 @@
// Build with
// v -use-coroutines simple_coroutines.v
//
import coroutines
import time
fn foo(a int) {
for {
println('hello from foo() a=${a}')
coroutines.sleep(1 * time.second)
}
}
fn foo2(a int) {
for {
println('hello from foo2() a=${a}')
coroutines.sleep(2 * time.second)
}
}
fn foo3(a int) {
for {
println('hello from foo3() a=${a}')
coroutines.sleep(3 * time.second)
}
}
fn main() {
go foo(10)
go foo2(20)
go foo3(30)
for {
println('hello from MAIN')
coroutines.sleep(1 * time.second)
}
println('done')
}

View File

@ -11,7 +11,7 @@ import time
#include "photonwrapper.h"
fn C.photon_init_default() int
fn C.photon_thread_create(f voidptr)
fn C.photon_thread_create(f voidptr, arg voidptr)
fn C.photon_sleep_s(n int)
fn C.photon_sleep_ms(n int)
@ -21,5 +21,8 @@ pub fn sleep(duration time.Duration) {
}
fn init() {
C.photon_init_default()
ret := C.photon_init_default()
if ret < 0 {
panic('failed to initialize coroutines via photon (ret=${ret})')
}
}