cameron314 / concurrentqueue

A fast multi-producer, multi-consumer lock-free concurrent queue for C++11
Other
9.52k stars 1.66k forks source link

try_enqueue still returns false #389

Closed fxsts closed 2 weeks ago

fxsts commented 3 weeks ago

Initialize the queue and define a large capacity, but try_enqueue still returns false in large numbers.The producer is high throughput, but only one producer. moodycamel::ConcurrentQueue qTick(1024102410); tick is a custom struct, 100 bytes long

cameron314 commented 3 weeks ago

Configure the implicit block index size. See e.g. https://github.com/cameron314/concurrentqueue/issues/379#issuecomment-2047361512

fxsts commented 3 weeks ago

moodycamel::ConcurrentQueue q(1024102410);

std::thread tProducer([&] {
    int val=0;
    while(true)
    {
        if (!q.try_enqueue(val++))
        {
            std::cout<<"try_enqueue failed ["<<val<<"]"<<std::endl;
        }
    }
});

std::thread tConsumer([&] {
    int val;
    while(true)
    {
        q.try_dequeue(val);
    }
});

tProducer.join();
tConsumer.join();

try_enqueue failed [1025] try_enqueue failed [2818] try_enqueue failed [5059] try_enqueue failed [6916] try_enqueue failed [8741] try_enqueue failed [11014]

cameron314 commented 3 weeks ago

Yes? The index is still not being sized through the traits.

fxsts commented 3 weeks ago

It may be the cause of the display in the web editor, correctly defined as such 无标题

cameron314 commented 3 weeks ago

Still using the default traits. Perhaps I should point to a more clear explanation. Have a look at https://github.com/cameron314/concurrentqueue/issues/169#issuecomment-546113196

I also suggest using the three-argument constructor.

fxsts commented 3 weeks ago

struct MyTraits : public moodycamel::ConcurrentQueueDefaultTraits { static const size_t BLOCK_SIZE = 1024; static const size_t EXPLICIT_INITIAL_INDEX_SIZE = 1024; };

无标题

try_enqueue still return false.Can you give me a demo example,thanks

cameron314 commented 3 weeks ago

Try

struct MyTraits {
    static const size_t BLOCK_SIZE = 1024;
    static const size_t IMPLICIT_INITIAL_INDEX_SIZE = 2048;
};

moodycamel::ConcurrentQueue<int, MyTraits> q(1024 * 1024, 0, 1);
fxsts commented 3 weeks ago

I'm sorry, but try_enqueue will also return false if you customize MyTraits using the method you provided

cameron314 commented 3 weeks ago

Works for me:

#include "../concurrentqueue.h"

#include <iostream>
#include <thread>

struct MyTraits : moodycamel::ConcurrentQueueDefaultTraits {
    static const size_t BLOCK_SIZE = 1024;
    static const size_t IMPLICIT_INITIAL_INDEX_SIZE = 2048;
};

moodycamel::ConcurrentQueue<int, MyTraits> q(1024 * 1024, 0, 1);

int main() {
    std::thread([&] {
        int val = 0;
        while (q.try_enqueue(val++));
        std::cout << "try_enqueue failed after [" << val-1 << "] elements inserted" << std::endl;
    }).join();
}

Prints out try_enqueue failed after [1049600] elements inserted.

fxsts commented 2 weeks ago

The total amount of data was around 1kw, the queue initialization capacity was defined as 1024102410, and there were consumers, but try_enqueuey started to report errors when it was over 3 million

cameron314 commented 2 weeks ago

For 10*1024*1024 elements at 1024 per block you need an implicit index size of at least 16*1024.

fxsts commented 2 weeks ago

Thank you very much. I set IMPLICIT_INITIAL_INDEX_SIZE=1024*16, in the case of a producer and a consumer, it will take about 1700 W before try_enqueue returns false. Is there a formula for calculating the relationship between BLOCK_SIZE, IMPLICIT_INITIAL_INDEX_SIZE and the number of elements? When setting BLOCK_SIZE and evaluating the number of elements, how should the IMPLICIT_INITIAL_INDEX_SIZE be calculated?

cameron314 commented 2 weeks ago

try_enqueue is not allowed to allocate memory. So you need enough blocks, and an index with enough entries for those blocks, to use try_enqueue exclusively.

The formula is just dividing the number of elements by the block size to get the number of blocks, then rounding up to a power of 2.

fxsts commented 2 weeks ago

thank you very much.