Closed fxsts closed 2 weeks ago
Configure the implicit block index size. See e.g. https://github.com/cameron314/concurrentqueue/issues/379#issuecomment-2047361512
moodycamel::ConcurrentQueue
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]
Yes? The index is still not being sized through the traits.
It may be the cause of the display in the web editor, correctly defined as such
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.
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
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);
I'm sorry, but try_enqueue will also return false if you customize MyTraits using the method you provided
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
.
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
For 10*1024*1024
elements at 1024 per block you need an implicit index size of at least 16*1024
.
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?
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.
thank you very much.
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