Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What is the best practices for qqueue shared among threads ?

What is the best practices for qqueue shared among threads ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by RahibeMeryem
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • R RahibeMeryem

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R Offline
        R Offline
        RahibeMeryem
        wrote on last edited by
        #3

        @jsulm

        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

        jsulmJ 1 Reply Last reply
        0
        • R RahibeMeryem

          @jsulm

          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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RahibeMeryem
            wrote on last edited by RahibeMeryem
            #5

            @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
            
            jsulmJ 1 Reply Last reply
            0
            • R RahibeMeryem

              @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
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #6

              @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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RahibeMeryem
                wrote on last edited by
                #7

                @jsulm

                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

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  RahibeMeryem
                  wrote on last edited by
                  #8

                  @jsulm

                  I put

                  QQueue <cv::Mat> shared_frames_que;
                  

                  in one one of the cpp but not worked also. (for definition)

                  same error

                  mrjjM 1 Reply Last reply
                  0
                  • R RahibeMeryem

                    @jsulm

                    I put

                    QQueue <cv::Mat> shared_frames_que;
                    

                    in one one of the cpp but not worked also. (for definition)

                    same error

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @RahibeMeryem
                    Hi

                    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.
                    
                    1 Reply Last reply
                    2
                    • R Offline
                      R Offline
                      RahibeMeryem
                      wrote on last edited by
                      #10

                      @mrjj

                      I will test soon let you know.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi,

                        You should rather follow @jsulm´s suggestion which is simpler to put it place.

                        Declare you mutexed queue as member of your main window and pass it to the producer and consumer.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved