rigtorp / MPMCQueue

A bounded multi-producer multi-consumer concurrent queue written in C++11
MIT License
1.15k stars 159 forks source link

cannot convert ‘Message*’ to ‘rigtorp::mpmc::Slot<Message>*’ #30

Closed mewais closed 2 years ago

mewais commented 2 years ago

Hi, I am trying to use MPMCQueue with a custom allocator. But I am getting the two following errors:

/Path/To/Project/Debug/external/include/rigtorp/MPMCQueue.h:129:33: error: cannot convert ‘Message*’ to ‘rigtorp::mpmc::Slot<Message>*’ in assignment
  129 |     slots_ = allocator_.allocate(capacity_ + 1);
/Path/To/Project/Debug/external/include/rigtorp/MPMCQueue.h:134:29: error: cannot convert ‘rigtorp::mpmc::Slot<Message>*’ to ‘CommMemoryAllocator<Message>::pointer’ {aka ‘Message*’}
  134 |       allocator_.deallocate(slots_, capacity_ + 1);

This is how I define the allocator and pass it to MPMCQueue:

template <typename T>
class CommMemoryAllocator
{
    public:
        typedef size_t size_type;
        typedef ptrdiff_t difference_type;
        typedef T* pointer;
        typedef const T* const_pointer;
        typedef T& reference;
        typedef const T& const_reference;
        typedef T value_type;

        T* allocate(size_type num, const void* hint = nullptr);

        void deallocate(pointer ptr, size_type num);
};

template <typename T>
using comm_mqueue = rigtorp::MPMCQueue<T, CommMemoryAllocator<T>>;

Then inside one of my classes I actually use it like so:

comm_mqueue<Message> downward_queue{20};

I don't actually use the queues yet (so no pushing or popping), I am getting these errors directly from this simple use.

Is there something I am missing or is this a bug?I am using Linux, gcc/g++ 11.1.0, and this is being compiled as C++17.

mewais commented 2 years ago

For completeness, the data structure Message is as follows:

class Message
{
    public:
        uint32_t destination;
        uint32_t source;
        uint32_t process;
        uint32_t tag;
        MessageType type;               // This is just an enum
        MessageDirection direction;     // an enum too
        uint32_t size;
        void* data;

        // constructor
        Message(uint32_t host, uint32_t process, uint32_t tag, MessageType type, MessageDirection direction, 
                uint32_t size, void* data);
        // constructor
        Message(uint32_t source, uint32_t process, uint32_t tag, MessageType type);
};
rigtorp commented 2 years ago

Allocator needs to be supplied like this: template <typename T, typename Allocator = mpmc::AlignedAllocator<mpmc::Slot>> using MPMCQueue = mpmc::Queue<T, Allocator>;

On Fri, Jul 30, 2021 at 4:28 PM Mohammad Ewais @.***> wrote:

For completeness, the data structure Message is as follows:

class Message { public: uint32_t destination; uint32_t source; uint32_t process; uint32_t tag; MessageType type; // This is just an enum MessageDirection direction; // an enum too uint32_t size; void* data;

    // constructor
    Message(uint32_t host, uint32_t process, uint32_t tag, MessageType type, MessageDirection direction,
            uint32_t size, void* data);
    // constructor
    Message(uint32_t source, uint32_t process, uint32_t tag, MessageType type);

};

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rigtorp/MPMCQueue/issues/30#issuecomment-889931089, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABLO25YG2YR7TOTZLXZSSTT2KZKLANCNFSM5BH7QMZQ .