lichao
2021-04-21 3931f83205f153f2bc7fc36d1a894cdc3f14b4db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * =====================================================================================
 *
 *       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