Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special channel features example needs set capacity to success #23481

Open
jorgeluismireles opened this issue Jan 15, 2025 · 2 comments
Open
Labels
Unit: Documentation Bugs/feature requests, that are related to the documentations.

Comments

@jorgeluismireles
Copy link

jorgeluismireles commented Jan 15, 2025

Describe the issue

The example at the doc don't push a channel successfully unless the channel cap is set.

Original code:

struct Abc {
    x int
}

a := 2.13
ch := chan f64{}
res := ch.try_push(a) // try to perform `ch <- a`
println(res)
l := ch.len // number of elements in queue
c := ch.cap // maximum queue length
is_closed := ch.closed // bool flag - has `ch` been closed
println(l)
println(c)
mut b := Abc{}
ch2 := chan Abc{}
res2 := ch2.try_pop(mut b) // try to perform `b = <-ch2`

Produces

not_ready
0
0

With the following change:

ch := chan f64{ cap:1 }

Produces what we expect:

success
1
1

Don't know if default capacity should not be zero (bug) or not. When you adapt this code for you don't get receptions. Then debug and found result not_ready and then when you set the capacity you get your messages.

Links

https://docs.vlang.io/concurrency.html#special-channel-features

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@jorgeluismireles jorgeluismireles added the Unit: Documentation Bugs/feature requests, that are related to the documentations. label Jan 15, 2025
Copy link

Connected to Huly®: V_0.6-21911

@jorgeluismireles
Copy link
Author

Another way to document try_push success is consuming first and leaving the channel unbuffered:

a := 2.13
ch := chan f64{}
spawn fn[ch]() {
	println('pop: ${<-ch}')
	exit(0)
}()
push := ch.try_push(a) // try to perform `ch <- a`
println('push:${push} len:${ch.len} cap:${ch.cap} closed:${ch.closed}')
for { }
push:success len:0 cap:0 closed:false
pop: 2.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unit: Documentation Bugs/feature requests, that are related to the documentations.
Projects
None yet
Development

No branches or pull requests

1 participant