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 :-) -- 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/a4c340b5-a576-4dbd-a973-679fd5b98812n%40googlegroups.com.