Tuesday, September 30, 2025
HomeGolangGetting unordered output for unbuffered channels - Getting Assist

Getting unordered output for unbuffered channels – Getting Assist


I inserted 2 values in a go channel and I can see the time of insertion as nicely.
1st worth is getting into earlier than 2nd, all the time.
Nevertheless, generally, the 2nd entry is listened 1st from the unbuffered channel.

Screenshot containing time of entry of course of 1 and three in time:nanosecond –

As we are able to see that 3 obtained listened first at 293187th nanosecond
after which 1st obtained listened,
Nevertheless 1st was inserted at 293313th nanosecond and 2nd at 293314th nanosecond

Can I see the code? Notice that, getting the timestamp just isn’t instantaneous, and a variety of issues can occur between getting time time and sending the merchandise to the channels.

Mainly Producer and client are referred to as in parallel, as they’re In a unique goroutine

Sure, and that’s why there isn’t a assure in regards to the ordering.
Primarily based on the code you offered above, I’m guessing the producer code is one thing like this:

go func() {
   ....
   p.logCh <- course of
   // it is truly potential that one other goroutine ship one other 
   // course of to p.logCh in several goroutine after the road above
   // and earlier than the log.Printf line beneath
   log.Printf("<the_timestamp> <the_process_id> despatched")
   ....
}

So, it’s potential that the circulation of your program is one thing like this:

  1. in 1st nanosecond you created 2 goroutine, goroutine A and goroutine B
  2. in 2nd nanosecond, goroutine A ship an merchandise X to the channel
  3. in third nanosecond, goroutine B ship an merchandise Y to the channel
  4. in 4th nanosecond, goroutine B log “4 Y ended and despatched to channel”
  5. in fifth nanosecond, goroutine A log “5 X ended and despatched to channel”

because of this, you will notice that Y logged earlier than X, however truly X is distributed first earlier than Y.

What’s the answer? Nicely, you need to assume that sending an merchandise to a channel and logging the occasion as a single operation that ought to be atomic (can’t be overlapped with different operation). A technique to do that is utilizing mutex. Earlier than sending an merchandise to a channel, lock the mutex first, then log the occasion, then unlock the mutex.

It’s not this, I’m asking about once I hear from the channel.
Should you examine the primary screenshot, A ought to be listened first based mostly in your rationalization that A entered first and B second.
Nevertheless the result’s that, B was pulled out first from the channel after which A.

It’s not in regards to the log, it’s about order by which they have been consumed.

Mainly,
A was despatched to channel at 1 th nanosecond, and B at 2 th nanosecond.
Nevertheless, B got here out at 3 rd nanosecond and A at 4th

How are you aware A despatched to the channel at 1st nanosecond and B at 2nd nanosecond?

You may examine my first screenshot,
1st course of was despatched after which third, nonetheless third got here out earlier than 1st from channel

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments