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
], 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.