What is the best practices for qqueue shared among threads ?
-
Hi,
I am still struggling to find a decent solution for my producer and workers system.
one of my thread producing cv::Mat s and ı want to consume this cv::Mat2s from 4 different threads.
I am really confused and lost ....
Is it possible to share a QQueue or a QVector among all threads and use mutex ? signal slots messaging is dropping the signals because workers not fast as producer.
any example could be very helpful
best..
EDIT 1: I looked zeromq and looks promising but seems complex. Decoupling workers and communicating with a fast messaging could be a solution also
-
@RahibeMeryem said in What is the best practices for qqueue shared among threads ?:
Is it possible to share a QQueue or a QVector among all threads and use mutex ?
Yes it is.
"signal slots messaging is dropping the signals because workers not fast as producer" if you share QQueue/QVector you don't need signals/slots and use https://doc.qt.io/qt-5/qtcore-threads-semaphores-example.html to block the producer if the queue is too long already. See https://en.wikipedia.org/wiki/Producer–consumer_problem#Using_semaphores
-
very stupid question :
my mainwindow.cpp starting threads with:
detector_thread = new QThread; videofeeder = new VideoFeeder; videofeeder->moveToThread(detector_thread); bc_thread_01 = new QThread; bc_worker_1 = new Bc_worker_detector; bc_worker_1->moveToThread (bc_thread_01); bc_thread_01->start(); bc_thread_02 = new QThread; bc_worker_2 = new Bc_worker_detector; bc_worker_2->moveToThread (bc_thread_02); bc_thread_02->start(); bc_thread_03 = new QThread; bc_worker_3 = new Bc_worker_detector; bc_worker_3->moveToThread (bc_thread_03); bc_thread_03->start ();
there is a Bc_worker-detector.h and Bc_worker-detector.cpp
Also there is a SharedVar.h just include:
extern QQueue <cv::Mat> shared_frames_que;
I declared it in videofeeder and include SharedVar.h
and messed up. not working
What is the proper way to share QQueue between seperate threads ? Really. I forget cpp these days due to python torture...
Best
-
@RahibeMeryem said in What is the best practices for qqueue shared among threads ?:
not working
What is not working?
You need the definition for shared_frames_que as well in some cpp file. -
@jsulm it gives :
Undefined symbols for architecture x86_64: "_shared_frames_que", referenced from: Bc_worker_detector::frame_detect_mace(cv::Mat const&) in bc_worker_detector.o ld: symbol(s) not found for architecture x86_64
-
@RahibeMeryem As I said you need a definition also.
Or simply put it as member in your MainWindow and pass pointer to it to your workers. -
don't remember how to do that at he moment... really . :(
would you mind to show it to me ? my girlish power ZERO today. almost giving up......
thnx a lot
-
I put
QQueue <cv::Mat> shared_frames_que;
in one one of the cpp but not worked also. (for definition)
same error
-
In some .h file extern QQueue <cv::Mat> shared_frames_que; (outside any functions/classes) and then in some cpp file (outside any functions) QQueue <cv::Mat> shared_frames_que; should just work. ( make sure it knows cv:Mat also ) Try to make clean All + rebuild from build menu. I just tested with extern QQueue <int> shared_frames_que; and QQueue <int> shared_frames_que; and it just compiled.
-
I will test soon let you know.