On Fri, May 4, 2018 at 2:00 AM, <bambys...@gmail.com> wrote: > > And I want to modify the worker thread like this: > > for { > select { > case wi := <- work_ch: > self.do_the_work(wi) > case <-self.idle_request: > self.idle_response <- true > } > } > > However this last snippet of code does not work because the cases within the > select statement are randomly chosen when both channels are ready so I have > no guarantee that the first case is executed first.
You also have no guarantee that a request won't arrive on work_ch a nanosecond after the worker is told to go idle, but presumably that is not a problem. If you need to ensure that some set of requests is completed before the workers go idle, then you need some sort of timestamp on jobs, and you should tell the worker to go idle as of a specific timestamp. Once you have that, when the worker sees an idle request, with an idle timestamp, it can keep accepting work requests until it sees one with a later timestamp. It can then report that it is going idle, and sleep until it is woken up, holding onto the next job that it will start doing when it wakes up. 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.