cameron314 / concurrentqueue

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

why ocassionally unexcepted clear queue #395

Open manuel76413 opened 2 months ago

manuel76413 commented 2 months ago

I write a parallel program that uses concurrent-queue, and most time it runs ok with very high performance. however, in an unknown situation, when the queue has about one million items, it will be empty in an unexcepted way, so I will lost many many jobs which should be in the queue.

my code is like :

moodycamel::BlockingConcurrentQueue m_fast_queue; bool br = m_fast_queue.enqueue(start_job); ... void worker(int tid) { std::string itm; while (!m_stop_workThreads.load()) { bool br = m_fast_queue.wait_dequeue_timed(itm, std::chrono::milliseconds(25)); if (br && !itm.empty()) { ++m_active_workThreads; do_job(itm); --m_active_workThreads; } } }

the information while run, I print to console window, like: -task:14733521-err:418891-queue:5103320 -task:14733521-err:418892-queue:5103297 -task:14733521-err:419156-queue:5103018 -task:14747550-err:419283-queue:0

the last row shows the queue size suddenly dropping to 0, queue size by call m_fast_queue.size_approx().

================= so anybody can give some advice, on why this happened?

cameron314 commented 2 months ago

Can you reproduce in a simple example program?

manuel76413 commented 2 months ago

Can you reproduce in a simple example program?

I cannot, this problem happens occasionally, so it's difficult enough for me to reproduce it. But when I run my concurrent task for a long time, this always happens several times.

I think maybe there's unexceptional in the work thread that caused this problem, but the best solution is how to protect the queue to avoid dropping to empty.

cameron314 commented 2 months ago

Sorry, this is the first time anything like this is reported. Even if it takes hours to occur, I will still need an isolated program that reproduces the issue in order to investigate further.