On Mon, 6 Nov 2017, at 12:14 PM, arunabh.ar...@gmail.com wrote:
> Hi,
> 
> I am fairly new to Golang, so please excuse if this is a silly
> question. I could not find any answers on the web.> 
> Code portion A:
> 
>   rf.mu.Lock()
>   rf.electionTicker =  time.NewTicker(rf.currentTimeout)
>   <============RACE>   rf.mu.Unlock()
> 
> Code portion B:
> for {
>   select {
>     case <-rf.electionTicker.C: // do something
>     <============RACE>     default:
>   }
> }
> 
> Can you please tell me why a channel read is facing a race condition.> Is 
> there a way to prevent this race?

The race is caused because you are writing to rf.electionTicker and then
reading from it to access the channel you want to read from. The channel
read itself is not causing the race.
You could read rf.electionTicker into a variable in some code guarded by
the mutex and read from that instead.

Ian

-- 
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