Timeout based concurrent queue in C++
I am trying to develop a concurrent queue using POSIX threads + C++ with the following semantics:
- Some threads may call push(item, timeout) and if the addition of the element does not happen by time timeout it must be rejected.
- Some threads may call pop(item, timeout) and again if deletion of the element does not happen by time timeout it must be rejected.
- Some threads will always call push(item) and pop(item) which means that these always get done -- no bounds on 开发者_JAVA技巧time is specified.
My questions:
- I believe it makes sense to prioritize the threads that have timeouts. So I am using pthread scheduling to allocate higher priority to threads that have timeouts. Is this the only approach I can take?
- Despite the altering of thread priorities some threads with timeouts have to be rejected. How to keep these at a minimum?
You should be very careful with tweaking thread priorities. The most obvious problem with the above strategy is that threads with no timeouts may suffer starvation. If a push doesn't have a timeout, that doesn't necessarily mean that it doesn't matter if its message gets sent.
Minimising timeouts is entirely the application's responsibility. If you don't want your writers to time out, makes sure the readers slurp up messages quickly enough.
I have to say, though, that I find the idea of mixing up timed and un-timed waiters (on the same side, at least) to be rather strange. Do you have some particular use case in mind?
check out boost.asio
Timer.5 - Synchronising handlers in multithreaded programs
精彩评论