/*
|
* =====================================================================================
|
*
|
* Filename: lock_free_queue.h
|
*
|
* Description:
|
*
|
* Version: 1.0
|
* Created: 2021年04月21日 14时03分27秒
|
* Revision: none
|
* Compiler: gcc
|
*
|
* Author: Li Chao (), lichao@aiotlink.com
|
* Organization:
|
*
|
* =====================================================================================
|
*/
|
|
#ifndef LOCK_FREE_QUEUE_KQWP70HT
|
#define LOCK_FREE_QUEUE_KQWP70HT
|
|
#include "shm.h"
|
#include <boost/interprocess/offset_ptr.hpp>
|
#include <boost/lockfree/queue.hpp>
|
|
using namespace bhome_shm;
|
|
typedef int64_t Data;
|
const int kQLen = 10;
|
class LockFreeQueue : private boost::lockfree::queue<Data,
|
boost::lockfree::allocator<Allocator<Data>>,
|
boost::lockfree::capacity<kQLen>>,
|
private boost::noncopyable
|
{
|
typedef boost::lockfree::queue<Data,
|
boost::lockfree::allocator<Allocator<Data>>,
|
boost::lockfree::capacity<kQLen>>
|
Queue;
|
|
public:
|
LockFreeQueue(SharedMemory &shm) :
|
Queue(shm.get_segment_manager()) {}
|
bool Read(Data &d) { return pop(d); }
|
bool Write(Data const &d) { return push(d); }
|
template <class Func>
|
bool Write(Data const &d, Func onWrite)
|
{
|
if (Write(d)) {
|
onWrite(d);
|
return true;
|
} else {
|
return false;
|
}
|
}
|
};
|
|
#endif // end of include guard: LOCK_FREE_QUEUE_KQWP70HT
|