jeudi 17 juin 2021

A lock-free batch buffer with multi-producer and single-consumer with c++11

I want a lock-free batch buffer with multi-producer and single-consumer, but I can't do it. The skeleton of code as follows. HELP WANTED!!

class MyBatch{
    public:
        atomic<bool> consumer_flag{flase};
    public:
        void push(char* data, uint32 data_len){
            while(cur_pos.load() + data_len >= max_buffer_size){
                consumer_flag.store(true);
                //and othres can use consumeAllBatch() to consume the whole batch data
            }
            memcpy(buffer + cur_pos.load(), data, data_len);
            cur_pos += data_len;
        };
        char* consumeAllBatch(){
            cur_pos.store(0);
        };
    private:
        char* buffer;
        atomic<uint32> cur_pos{0};
        uint32 max_buffer_size{1<<16};
}

Aucun commentaire:

Enregistrer un commentaire