views:

58

answers:

2

I am trying to develop a concurrent queue using POSIX threads + C++ with the following semantics:

  1. Some threads may call push(item, timeout) and if the addition of the element does not happen by time timeout it must be rejected.
  2. Some threads may call pop(item, timeout) and again if deletion of the element does not happen by time timeout it must be rejected.
  3. Some threads will always call push(item) and pop(item) which means that these always get done -- no bounds on time is specified.

My questions:

  1. 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?
  2. Despite the altering of thread priorities some threads with timeouts have to be rejected. How to keep these at a minimum?
A: 

check out boost.asio

Timer.5 - Synchronising handlers in multithreaded programs

plan9assembler
+1  A: 

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?

Marcelo Cantos