On Thu, Feb 21, 2019 at 12:48 PM Serhat Şevki Dinçer <jfcga...@gmail.com> wrote:
>
> Hi rog,
>
> As -> and <- are 'not racy', because they are 'core' language features, 
> because runtime handles them, because they are guaranteed to 'work', I am 
> asking how easy and useful to implement them as 'core' language features.

Those operations are not racy by their nature, not because they are
core features. Channels are synchronization tools, so read from a
channel only works if there is a write waiting, or the buffer is
nonempty.

You can implement waitempty(ch) without a race condition. However, any
scenario in which you use waitempty(ch) would have a race condition,
because the moment it returns there is no guarantee on the state of
the channel. So in a piece of code like this:

waitempty(ch)
post


the only guarantee you have is that when 'post' is running, ch was
empty at least once since waitempty was called.

This code:

for {
  if waitempty(ch) {
    ...
  }
}

is racy, because when you go into the if block, you have no idea how
many times ch was empty.




>
> Thank you..
>
> On Thursday, February 21, 2019 at 9:04:22 PM UTC+3, rog wrote:
>>
>> As others have said, such operations are inherently racy - when you receive 
>> the message that a channel is empty, it may not be empty any more. For that 
>> reason, it's not a good idea to add them as primitives to the language.
>>
>> That said, it's not too hard to implement them yourself. We are talking 
>> about buffered channels here, so an extra item being buffered should be OK. 
>> So you can insert a goroutine in between sender and receiver that signals 
>> when it can't receive on a channel (it's empty) or when it can't send on a 
>> channel (it's full).
>>
>> Here's some code that implements those operations:
>>
>>     https://play.golang.org/p/2xyZnUI0imX
>>
>> Note that there are many other possible variants. For example, you might 
>> want to send on the notification channel only when the channel has been idle 
>> for some time.
>>
>>   cheers,
>>     rog.
>>
>>
>> On Thu, 21 Feb 2019 at 13:38, Serhat Şevki Dinçer <jfcg...@gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> Say you have a buffered channel
>>> ch := make(chan int, 10)
>>>
>>> Would it make sense and be easy to add the following directives to the Go 
>>> language core?
>>>
>>> waitempty(ch)
>>> blocks caller until ch buffer is empty
>>>
>>> waitfull(ch)
>>> blocks caller until ch buffer is full
>>>
>>> This means there are 3 types of goroutines related to a channel:
>>> - senders
>>> - receivers
>>> - observers
>>>
>>> There are nice possibilities with these, for example wait groups, resource 
>>> usage alerts etc.
>>>
>>> What do you think?
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "golang-nuts" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to golang-nuts...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to