Question

I declare spsc_queue like this:

boost::lockfree::spsc_queue<fut_sess_contents,  boost::lockfree::capacity<1024>> futInfoUpdates;

I process it this way:

fut_sess_contents entry_fsc;
while (futInfoUpdates.pop(entry_fsc))
{
    .... work
}

I assume I pass entry_fsc here by reference and spsc_queue memcpy next element. It does not look effective, I would prefer just get pointer to the item in storage and work with item from storage directly(avoiding extra memcpy). Can I do this somehow? My storage is big enough and I process it fast enough so I'm sure that I can work with pointer from storage directly.

Was it helpful?

Solution

You can't.

This is exactly required so the SPSC queue can be implemented in a lockfree fashion (assuming fixed capacity).

You can't have your cake and eat it, too

Also, you're way past micro-optimizing.

Did your profiler tell you this is your performance bottleneck? (Hint: No, it didn't).

OTHER TIPS

Once an item has been popped from the queue, its content may be overwritten at any point in time; and sooner rather than later if the queue is full or close to full.

Therefore, it is essential that the content be moved/copied out before the item is marked as popped, otherwise you would get memory corruption.

If you want just a pointer to the element... the solution is to make a queue of pointers. Define your queue over a boost::shared_ptr<fut_sess_contents> and just that pointer will be copied (in C++11, if supported, you should prefer a std::unique_ptr<fut_sess_contents>).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top