Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blocking peek/pop? #20

Open
golightlyb opened this issue Mar 16, 2020 · 0 comments
Open

blocking peek/pop? #20

golightlyb opened this issue Mar 16, 2020 · 0 comments

Comments

@golightlyb
Copy link

golightlyb commented Mar 16, 2020

Is it possible to block on peek or pop?

Currently I'm just polling in a loop, but I guess in the empty-queue case its lightweight enough: q.RLock() then q.getItemByID(...) => q.Length() => q.tail - q.head, so basically just a lock and a comparison

If it makes the use case easier. I am writing/enqueing from multiple places but only reading/dequeing from one place. I don't mind spurious wakeups and I don't need to race against other goroutines which could also be waiting - I just want to block until the queue is not empty.

Many thanks

func foo(q *goque.Queue) {
    ticker := time.NewTicker(pollPeriod)
    defer ticker.Stop()
    
    for {
        select {
            case <-ticker.C: // blocks
        }
        
        item, err := q.Peek()
        if err == goque.ErrEmpty { continue }
        if err != nil { panic(err) }
        
        fmt.Println(item.ToString())
    }
}

func main() {
   // ...
   go foo(q)
   // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant