multithreading - Is there a reliable way to ensure a Go channel does not block on read? -
this followup a previous thread similar name.
it has accepted answer, answer not answer question. thread, here use-case:
if len(mychannel) > 0 { // possible issue here: length have changed 0 making blocking elm := <- mychannel return elm } the op calls "possible issue", it's definite issue: race condition in consumer may have pulled value channel between evaluation of if condition , execution of 2 statements.
now, told go way favor channels on mutex, here seems can not acheive basic non-blocking read (by polling length , reading atomically) without pairing mutex , channel together, , using our new concurrency data type instead of channel.
can right? there no way reliably ensure recv not block checking ahead space? (compare blockingqueue.poll() in java, or similar facilities in other queue-based messaging ipc facilities...)
this default cases in select for:
var elm mytype select { case elm = <-mychannel: default: } return elm this assigns elm if can, , otherwise returns 0 value. see "a leaky buffer" effective go more extensive example.
Comments
Post a Comment