Brian Candler schrieb am Mittwoch, 12. Mai 2021 um 10:34:31 UTC+2:
> On Wednesday, 12 May 2021 at 08:40:36 UTC+1 Haddock wrote: > >> I once implemented something to solve the same problem in Java. So I have >> a high priority queue (hq) and a low priority queue (lq). Whenever an item >> is atted to the hq a token is also added to some associated high priority >> token queue (htq). Same for the lq and ltq. >> >> Some thread or goroutine takes tokens from the htq as long as in contains >> tokens. You can add a count so that after any n tokens taken from the htq >> the ltq is checked whether it contains a token. >> >> Works fine until both, htp and ltp, are empty. You don't want to iterate >> over htp and ltp all the time till one contains a token again. So I >> introduced a semaphore on which the thread serving all these queues would >> be blocked till htq ot ltq recevie a new token. >> >> I thought that the sempahore would slow things down. So I did >> measurements with a bare bone concurrent queue and the priority queue. The >> difference in time to serve n tokens in htq and m tokens in ltq was very >> small also for high values of n and m. >> > > I think you'll enjoy this video by Rob Pike: > https://www.youtube.com/watch?v=5zXAHh5tJqQ > > Great ways to handle this stuff robustly using Go channels instead of > low-level synchronization primatives like semaphores. First time watching, > I had to keep my finger on the pause button to make sure I understood each > screenful of code before he moved on :-) > In addition to htq and ltq you have a third queue into which you also insert a token once one has been added to htq or ltp. The thread/goroutine that serves htq, ltp, hq, lq is blocked by this additional queue. When it is empty, htq and ltq are also empty. So when the goroutine is blocked it is fine in this case. This sound like this could be it :-) -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f0c33314-20aa-4842-a489-68c3c71d446dn%40googlegroups.com.