mirror of
https://github.com/bloeys/cogo.git
synced 2025-12-29 08:58:19 +00:00
New much better algorithm
This commit is contained in:
70
demo.go
70
demo.go
@ -23,41 +23,73 @@ func runDemo() {
|
||||
}
|
||||
|
||||
func test(c *cogo.Coroutine[int, int]) {
|
||||
if cogo.HasGen() {
|
||||
test_cogo(c)
|
||||
return
|
||||
}
|
||||
|
||||
c.Begin()
|
||||
|
||||
println("test yield:", 1)
|
||||
c.Yield(1)
|
||||
|
||||
if c.Out > 2 {
|
||||
c.Yield(1)
|
||||
}
|
||||
|
||||
// Yield here until at least 100ms passed
|
||||
c.YieldTo(cogo.NewSleeper(100 * time.Millisecond))
|
||||
|
||||
// Yield here until the coroutine 'test2' has finished
|
||||
c.YieldTo(cogo.New(test2, 0))
|
||||
// c.YieldTo(cogo.New(test2, 0))
|
||||
|
||||
println("test yield:", 2)
|
||||
c.Yield(2)
|
||||
}
|
||||
|
||||
func test2(c *cogo.Coroutine[int, int]) {
|
||||
if cogo.HasGen() {
|
||||
test2_cogo(c)
|
||||
return
|
||||
// func test2(c *cogo.Coroutine[int, int]) {
|
||||
|
||||
// println("test2222 yield:", 1)
|
||||
// c.Yield(1)
|
||||
|
||||
// println("test2222 yield:", 2)
|
||||
// c.Yield(2)
|
||||
|
||||
// println("test2222 before yield none")
|
||||
// c.YieldNone()
|
||||
// println("test2222 after yield none")
|
||||
// }
|
||||
|
||||
func NewApproach(state int) {
|
||||
|
||||
switch state {
|
||||
case 1:
|
||||
goto lbl_1
|
||||
case 2:
|
||||
goto lbl_2
|
||||
case 3:
|
||||
state = 1
|
||||
goto lbl_2
|
||||
default:
|
||||
}
|
||||
|
||||
c.Begin()
|
||||
println("1")
|
||||
println("2")
|
||||
state = 1
|
||||
// return
|
||||
|
||||
println("test2222 yield:", 1)
|
||||
c.Yield(1)
|
||||
lbl_1:
|
||||
println("3")
|
||||
state = 2
|
||||
// return
|
||||
|
||||
println("test2222 yield:", 2)
|
||||
c.Yield(2)
|
||||
lbl_2:
|
||||
{
|
||||
switch state {
|
||||
case 1:
|
||||
goto lbl_3
|
||||
|
||||
println("test2222 before yield none")
|
||||
c.YieldNone()
|
||||
println("test2222 after yield none")
|
||||
default:
|
||||
}
|
||||
|
||||
println("4")
|
||||
state = 3
|
||||
// return
|
||||
|
||||
lbl_3:
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user