11

So i'm using a boost::lockfree::spec_queue to communicate via two boost_threads running functors of two objects in my application.

All is fine except for the fact that the spec_queue::pop() method is non blocking. It returns True or False even if there is nothing in the queue. However my queue always seems to return True (problem #1). I think this is because i preallocate the queue.

typedef boost::lockfree::spsc_queue<q_pl, boost::lockfree::capacity<100000> > spsc_queue;

This means that to use the queue efficiently i have to busy wait constantly popping the queue using 100% cpu. Id rather not sleep for arbitrary amounts of time. I've used other queues in java which block until an object is made available. Can this be done with std:: or boost:: data structures?

easytiger
  • 514
  • 5
  • 15
  • Can you point out which concurrent queues in Java had a blocking pop? Chances are big that (a) it wasn't lockfree (b) it had tunables that specify exactly your dreaded "arbitrary amounts of time" – sehe Mar 18 '14 at 18:05
  • Yeah - just use a BlockingQueue class. You want your cake and eat it too? – Martin James Mar 18 '14 at 18:27

2 Answers2

10

A lock free queue, by definition, does not have blocking operations.

How would you synchronize on the datastructure? There is no internal lock, for obvious reasons, because that would mean all clients need to synchronize on it, making it your grandfathers locking concurrent queue.

So indeed, you will have to devise a waiting function yourself. How you do this depends on your use case, which is probably why the library doesn't supply one (disclaimer: I haven't checked and I don't claim to know the full documentation).

So what can you do:

  • As you already described, you can spin in a tight loop. Obviously, you'll do this if you know that your wait condition (queue non-empty) is always going to be satisfied very quickly.

  • Alternatively, poll the queue at a certain frequency (doing micro-sleeps in the mean time). Scheduling a good good frequency is an art: for some applications 100ms is optimal, for others, a potential 100ms wait would destroy throughput. So, vary and measure your performance indicators (don't forget about power consumption if your application is going to be deployed on many cores in a datacenter :)).

Lastly, you could arrive at a hybrid solution, spinning for a fixed number of iterations, and resorting to (increasing) interval polling if nothing arrives. This would nicely support servers applications where high loads occur in bursts.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks, I guess what I was asking is is there a blocking queue in boost. I'll leave it busy waiting for now. I can come back to it later. – easytiger Mar 19 '14 at 15:05
  • There's http://www.boost.org/doc/libs/1_36_0/boost/interprocess/ipc/message_queue.hpp but nothing more general AFAICT. Although just using a queue + condition-variable under a mutex seems simple enough. Add a counter and you have the logical equivalent of a semaphore for MPMC scenarios – sehe Mar 19 '14 at 15:15
  • "condition-variable under a mutex" could you please share this example? – Oleg Vazhnev Aug 28 '14 at 09:07
  • @javapowered quickly, [this](http://stackoverflow.com/a/23117587/85371) looks like it's close – sehe Aug 28 '14 at 09:10
  • 2
    @sehe thanks, it would be nice if you have the code to surrond `boost::lockfree::spsc_queue` with "condition-variable under a mutex" to make "blocking pop" – Oleg Vazhnev Aug 28 '14 at 09:14
  • @javapowered It would be the same, but you'd lose the advantage of lockless programming. Other projects have taken the "spin for a limited period of time, then progressive microsleeps to prevent CPU power drain" approach with good success. Don't remember the source, but maybe it was Disruptor Library that first blogged about this (google for LMAX?) – sehe Aug 28 '14 at 09:37
  • it seems I have to loose "advantage of lockless programming" if I don't want to burn CPU. "progressive microsleeps to prevent CPU power drain" doesn't work for me as good latency (fast response) is one of the major requirements in my case. I think i need something simple - just wait until data arrives and that's it. – Oleg Vazhnev Aug 28 '14 at 11:11
  • 2
    @javapowered Yes. IFF that leads to best latency. If load is consistently high enough to saturate a CPU core (say, >60%) you could elect to pay the extra power consumption of spinning to squeeze out just that tiny extra responsiveness. However, most server applications are dominated by network latency and lockfree queuing only makes sense if you expect to be saturated basically all of the time. – sehe Aug 28 '14 at 11:17
  • @sehe interesting comment re 60%. In the case iw as describing I have 1 process consuming data from 20 others so it likely would be busy all the time anyway and happily the system works very well as is. Thankfully its a high powered server and not a laptop or a smartphone ;) – easytiger Sep 10 '14 at 09:24
0

Use a semaphore to cause the producers to sleep when the queue is full, and another semaphore to cause the consumers to sleep when the queue is empty. when the queue is neither full nor empty, the sem_post and sem_wait operations are nonblocking (in newer kernels)

#include <semaphore.h>

template<typename lock_free_container>
class blocking_lock_free
{
public:
    lock_free_queue_semaphore(size_t n) : container(n)
    {
        sem_init(&pop_semaphore, 0, 0);
        sem_init(&push_semaphore, 0, n);
    }

    ~lock_free_queue_semaphore()
    {
        sem_destroy(&pop_semaphore);
        sem_destroy(&push_semaphore);
    }

    bool push(const lock_free_container::value_type& v)
    {
        sem_wait(&push_semaphore);
        bool ret = container::bounded_push(v);
        ASSERT(ret);
        if (ret)
            sem_post(&pop_semaphore);
        else
            sem_post(&push_semaphore); // shouldn't happen
        return ret;
    }

    bool pop(lock_free_container::value_type& v)
    {
        sem_wait(&pop_semaphore);
        bool ret = container::pop(v);
        ASSERT(ret);
        if (ret)
            sem_post(&push_semaphore);
        else
            sem_post(&pop_semaphore); // shouldn't happen
        return ret;
    }
private:
    lock_free_container container;
    sem_t pop_semaphore;
    sem_t push_semaphore;
};
Shloim
  • 5,281
  • 21
  • 36
  • turning to the kernel to resolve contention defeats the point of a lock-free queue – Dev Null Jul 20 '18 at 14:11
  • We only go to the kernel when the queue is either empty or full in order to avoid a busy wait. It's the best of both worlds. – Shloim Jul 21 '18 at 15:04
  • in low latency applications it is unacceptable and busy loop is the only option. http://ithare.com/infographics-operation-costs-in-cpu-clock-cycles/ - atomics are 15-30 cpu cycles, a kernel call is 1000-1500. If there's no requirement to be able to react faster than the latter, a futex-based queue that incorporates both worlds internally could be an easier choice. – Dev Null Jul 21 '18 at 22:22
  • The question was for a blocking pop in a lockfree, this is blocking only when the queue is full/empty, that's the only time a kernel call would be made. You can add a cycle of sem_trywait if you want to spin a little before blocking – Shloim Aug 28 '18 at 12:01