Boost logo

Boost Users :

Subject: Re: [Boost-users] why lockfree/queue.hpp requires has_trivial_assign and has_trivial_destructor to template type T
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2014-08-05 20:02:04


On 5/08/2014 18:48, 何子杰Hzj_jie wrote:
> it looks like an implementation limitation to me. the pointer surely
> works, but it will cause trouble about the instance lifetime.

It's not that much trouble. Allocate the pointer when you enqueue
(delete it if the enqueue fails) and pass it to std::unique_ptr as soon
as it dequeues. The only other thing to remember is to dequeue and
delete anything remaining in the queue in the destructor. (You may want
to raise an error or something in this case anyway, as it means some
tasks didn't get executed.)

> my scenario is a threadpool, which will work on functions / lambdas
> [function<bool(void)>], so let the threadpool queue keeps the instances
> would be an easy approach. but multi-produce-multi-consume is required.

If you're not wedded to lock-free, you might want to look at Boost.Asio.
  It's lock-based but it's trivial to create a generic threadpool around
its io_service, and it's very well optimised.

I have created a lock-free mini-io_service for some of my own code but
it's quite specialised and not really intended for general use.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net