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. Semaphore and multithread
Forum Updated to NodeBB v4.3 + New Features

Semaphore and multithread

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 5 Posters 3.0k Views
  • 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.
  • M Offline
    M Offline
    Montanaro
    wrote on 30 Jan 2020, 14:15 last edited by Montanaro
    #1

    Hi
    I have a simple app with one function reading interrupt and with ui.label writing the required value

    I have to divide the function(that checks interrupt) and the ui-label.

    Expert suggest me to put semaphore in.

    How can I do ? 
    
    
    *MainWindow::MainWindow(QWidget* parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
       
        qDebug() << "Program started!"
    
        QTimer* timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(poll()));
        //timer->start(1000);
        timer->start(500);
    }
    
    
    
    unsigned char MainWindow::poll()
    {
    
    //------------------------------------------------------------------------------
    unsigned char MainWindow::poll()
    {
        unsigned short distance;
            qDebug() << " Polling started";
    
             check_for_interrupt(ADDR, &distance); 
            
                qDebug() << "valore distanza:" << distance;
     
                ui->label->setText("il valore della distanza in mm è: " + QString::number(distance));
    
        
            }
    
    --------------------*
    
    J J 2 Replies Last reply 30 Jan 2020, 14:17
    0
    • M Montanaro
      30 Jan 2020, 14:15

      Hi
      I have a simple app with one function reading interrupt and with ui.label writing the required value

      I have to divide the function(that checks interrupt) and the ui-label.

      Expert suggest me to put semaphore in.

      How can I do ? 
      
      
      *MainWindow::MainWindow(QWidget* parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
         
          qDebug() << "Program started!"
      
          QTimer* timer = new QTimer(this);
          connect(timer, SIGNAL(timeout()), this, SLOT(poll()));
          //timer->start(1000);
          timer->start(500);
      }
      
      
      
      unsigned char MainWindow::poll()
      {
      
      //------------------------------------------------------------------------------
      unsigned char MainWindow::poll()
      {
          unsigned short distance;
              qDebug() << " Polling started";
      
               check_for_interrupt(ADDR, &distance); 
              
                  qDebug() << "valore distanza:" << distance;
       
                  ui->label->setText("il valore della distanza in mm è: " + QString::number(distance));
      
          
              }
      
      --------------------*
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 30 Jan 2020, 14:17 last edited by jsulm
      #2

      @Montanaro said in Semaphore and multithread:

      I have to divide the function(that checks interrupt) and the ui-label.

      What do you mean by "devide"?
      How are "Semaphore and multithread" involved here? I can't see anything in the code you posted.

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

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Montanaro
        wrote on 30 Jan 2020, 14:31 last edited by Montanaro
        #3

        Well, now I have only one thread.

        But ui.label and check_for_interrupt use the same channel.
        So maybe the ui.label and check_for_interrupt collide-

        So I need to put ui.label in one thread and check_for_interrupt in other thread and lock/unlock the channel/device they use.

        J 1 Reply Last reply 30 Jan 2020, 14:33
        0
        • M Montanaro
          30 Jan 2020, 14:31

          Well, now I have only one thread.

          But ui.label and check_for_interrupt use the same channel.
          So maybe the ui.label and check_for_interrupt collide-

          So I need to put ui.label in one thread and check_for_interrupt in other thread and lock/unlock the channel/device they use.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 30 Jan 2020, 14:33 last edited by
          #4

          @Montanaro Not sure what you mean by "channel".
          Is check_for_interrupt a blocking call? If not you do not need a second thread.

          If you do need a second thread, then put check_for_interrupt there and emit a signal when you get new data. Connect that signal to a slot in your MainWindow where you then update the label.

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

          M 1 Reply Last reply 30 Jan 2020, 15:05
          2
          • J jsulm
            30 Jan 2020, 14:33

            @Montanaro Not sure what you mean by "channel".
            Is check_for_interrupt a blocking call? If not you do not need a second thread.

            If you do need a second thread, then put check_for_interrupt there and emit a signal when you get new data. Connect that signal to a slot in your MainWindow where you then update the label.

            M Offline
            M Offline
            Montanaro
            wrote on 30 Jan 2020, 15:05 last edited by Montanaro
            #5

            @jsulm

            yes. I think the use the same channel (frame buffer).

            You suggest this was?

            unsigned char MainWindow::poll()
            {
            unsigned short m_distance;
            unsigned short distance;
            qDebug() << " Polling started";

                 check_for_interrupt(ADDR, &distance); 
                
                    qDebug() << "distance value:" << distance;  
            

            if(distance!=m_distance)
            { m_distance=distance;
            emit ui->label->setText("the value of distance is mm : " + QString::number(distance));
            }

                }
            
            JonBJ J 2 Replies Last reply 30 Jan 2020, 15:39
            0
            • M Montanaro
              30 Jan 2020, 15:05

              @jsulm

              yes. I think the use the same channel (frame buffer).

              You suggest this was?

              unsigned char MainWindow::poll()
              {
              unsigned short m_distance;
              unsigned short distance;
              qDebug() << " Polling started";

                   check_for_interrupt(ADDR, &distance); 
                  
                      qDebug() << "distance value:" << distance;  
              

              if(distance!=m_distance)
              { m_distance=distance;
              emit ui->label->setText("the value of distance is mm : " + QString::number(distance));
              }

                  }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 30 Jan 2020, 15:39 last edited by
              #6

              @Montanaro
              Assuming check_for_interrupt() is non-blocking (returns with an answer immediately) this is OK, if there's no other way to check fro the interrupt. If it blocks then this is not OK.

              Your code would be "nicer" if you emitted a signal when the interrupt arrives instead of putting your code for what to do into MainWindow::poll(), but that's up to you.

              If this is the actual code of that method, then the (misnamed here) m_distance local variable is not initialized. I would expect the compiler to warn you about this.

              1 Reply Last reply
              1
              • M Montanaro
                30 Jan 2020, 15:05

                @jsulm

                yes. I think the use the same channel (frame buffer).

                You suggest this was?

                unsigned char MainWindow::poll()
                {
                unsigned short m_distance;
                unsigned short distance;
                qDebug() << " Polling started";

                     check_for_interrupt(ADDR, &distance); 
                    
                        qDebug() << "distance value:" << distance;  
                

                if(distance!=m_distance)
                { m_distance=distance;
                emit ui->label->setText("the value of distance is mm : " + QString::number(distance));
                }

                    }
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 30 Jan 2020, 15:41 last edited by
                #7

                @Montanaro said in Semaphore and multithread:

                You suggest this was?

                No.
                Your code is not even valid:

                emit ui->label->setText("the value of distance is mm : " + QString::number(distance));
                

                This is not how signals/slots work.
                Please read https://doc.qt.io/qt-5/signalsandslots.html

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

                M 1 Reply Last reply 30 Jan 2020, 16:02
                2
                • J jsulm
                  30 Jan 2020, 15:41

                  @Montanaro said in Semaphore and multithread:

                  You suggest this was?

                  No.
                  Your code is not even valid:

                  emit ui->label->setText("the value of distance is mm : " + QString::number(distance));
                  

                  This is not how signals/slots work.
                  Please read https://doc.qt.io/qt-5/signalsandslots.html

                  M Offline
                  M Offline
                  Montanaro
                  wrote on 30 Jan 2020, 16:02 last edited by
                  #8

                  @jsulm
                  mmmm I can write:

                  Mainwindows writing {
                  ui->label->setText("the value of distance is mm : " + QString::number(distance))
                  }

                  and then :

                  unsigned char MainWindow::poll()
                  {
                  unsigned short m_distance;
                  unsigned short distance;
                  qDebug() << " Polling started";
                  check_for_interrupt(ADDR, &distance);

                      qDebug() << "distance value:" << distance;  
                  

                  if(distance!=m_distance)
                  { m_distance=distance;
                  emit writing
                  }
                  }

                  ?

                  Anyway, I m not using semaphore (unlock/lock) but I must use semaphore.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 30 Jan 2020, 17:44 last edited by
                    #9

                    @Montanaro said in Semaphore and multithread:

                    but I must use semaphore.

                    Why?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    M 1 Reply Last reply 31 Jan 2020, 00:52
                    1
                    • Christian EhrlicherC Christian Ehrlicher
                      30 Jan 2020, 17:44

                      @Montanaro said in Semaphore and multithread:

                      but I must use semaphore.

                      Why?

                      M Offline
                      M Offline
                      Montanaro
                      wrote on 31 Jan 2020, 00:52 last edited by
                      #10

                      @Christian-Ehrlicher
                      It' s my task.

                      ui.label and check_for_interrupt use the same resource(frame buffer).
                      Now when I use only ui.label, the code works; when I use only check_for_interrupt, the code works
                      but when I use together, the code works bad: the results of check_for_interrupt (this function read a time of flight sensor) is blocked on only one value and it's phisically not possible.
                      So I have to show if they collide and then I have to solve the problem.
                      I think to use semaphore to assign the resource.

                      J 1 Reply Last reply 31 Jan 2020, 05:15
                      0
                      • M Montanaro
                        31 Jan 2020, 00:52

                        @Christian-Ehrlicher
                        It' s my task.

                        ui.label and check_for_interrupt use the same resource(frame buffer).
                        Now when I use only ui.label, the code works; when I use only check_for_interrupt, the code works
                        but when I use together, the code works bad: the results of check_for_interrupt (this function read a time of flight sensor) is blocked on only one value and it's phisically not possible.
                        So I have to show if they collide and then I have to solve the problem.
                        I think to use semaphore to assign the resource.

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 31 Jan 2020, 05:15 last edited by
                        #11

                        @Montanaro You still did not answer this question: is check_for_interrupt blocking or not?
                        As you do not use threads in the code you posted I don't see the need for semaphores at all.
                        First you call check_for_interrupt, then ui->label->setText - they are NOT executed at the same time.

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

                        M 1 Reply Last reply 31 Jan 2020, 08:47
                        2
                        • J jsulm
                          31 Jan 2020, 05:15

                          @Montanaro You still did not answer this question: is check_for_interrupt blocking or not?
                          As you do not use threads in the code you posted I don't see the need for semaphores at all.
                          First you call check_for_interrupt, then ui->label->setText - they are NOT executed at the same time.

                          M Offline
                          M Offline
                          Montanaro
                          wrote on 31 Jan 2020, 08:47 last edited by
                          #12

                          @jsulm

                          Well … I dont know if check_for_interrupt blocks the resource. I suppose that and I want to detect that.

                          I think I can use that:
                          https://doc.qt.io/archives/qt-4.8/qmutexlocker.html

                          J M 2 Replies Last reply 31 Jan 2020, 09:20
                          0
                          • M Montanaro
                            31 Jan 2020, 08:47

                            @jsulm

                            Well … I dont know if check_for_interrupt blocks the resource. I suppose that and I want to detect that.

                            I think I can use that:
                            https://doc.qt.io/archives/qt-4.8/qmutexlocker.html

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 31 Jan 2020, 09:20 last edited by
                            #13

                            @Montanaro said in Semaphore and multithread:

                            check_for_interrupt blocks the resource

                            I'm not talking about blocking a resource. I'm simply asking whether check_for_interrupt blocks for longer time or returns immediately.
                            And again: why do you think you need semaphore if you do not use any threads?

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

                            1 Reply Last reply
                            0
                            • M Montanaro
                              30 Jan 2020, 14:15

                              Hi
                              I have a simple app with one function reading interrupt and with ui.label writing the required value

                              I have to divide the function(that checks interrupt) and the ui-label.

                              Expert suggest me to put semaphore in.

                              How can I do ? 
                              
                              
                              *MainWindow::MainWindow(QWidget* parent) :
                                  QMainWindow(parent),
                                  ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                              
                                 
                                  qDebug() << "Program started!"
                              
                                  QTimer* timer = new QTimer(this);
                                  connect(timer, SIGNAL(timeout()), this, SLOT(poll()));
                                  //timer->start(1000);
                                  timer->start(500);
                              }
                              
                              
                              
                              unsigned char MainWindow::poll()
                              {
                              
                              //------------------------------------------------------------------------------
                              unsigned char MainWindow::poll()
                              {
                                  unsigned short distance;
                                      qDebug() << " Polling started";
                              
                                       check_for_interrupt(ADDR, &distance); 
                                      
                                          qDebug() << "valore distanza:" << distance;
                               
                                          ui->label->setText("il valore della distanza in mm è: " + QString::number(distance));
                              
                                  
                                      }
                              
                              --------------------*
                              
                              J Offline
                              J Offline
                              J.Hilk
                              Moderators
                              wrote on 31 Jan 2020, 09:30 last edited by
                              #14

                              @Montanaro

                              here's the thing, multithreading /parallelization is not trivial. You'll need a good understanding of what you're doing or you end up, at best, wasting lots of time or at worst you shoot yourself in the foot.

                              Expert suggest me to put semaphore in.

                              I highly doubt that expert status. What is the reasoning behind that statement, please explain

                              think I can use that:
                              https://doc.qt.io/archives/qt-4.8/qmutexlocker.html

                              The point of a Semaphore is not to use a mutex

                              does check_for_interrupt actually take time at all ? And if it does how long. And what does it do exactly. There are other ways to do this, much more neater/higher level ones that mutex or semaphores especially in Qt


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              M 1 Reply Last reply 31 Jan 2020, 09:43
                              0
                              • M Montanaro
                                31 Jan 2020, 08:47

                                @jsulm

                                Well … I dont know if check_for_interrupt blocks the resource. I suppose that and I want to detect that.

                                I think I can use that:
                                https://doc.qt.io/archives/qt-4.8/qmutexlocker.html

                                M Offline
                                M Offline
                                Montanaro
                                wrote on 31 Jan 2020, 09:31 last edited by Montanaro
                                #15

                                @jsulm
                                well Experts tell me that code (ui or check_for_interrupt) can occupy the resource while the other line of code ( ui or check_for_interrupt) starts. I 'm beginner so I dont know if it's true but it's mandatory to check that for me.

                                JonBJ 1 Reply Last reply 31 Jan 2020, 09:34
                                0
                                • M Montanaro
                                  31 Jan 2020, 09:31

                                  @jsulm
                                  well Experts tell me that code (ui or check_for_interrupt) can occupy the resource while the other line of code ( ui or check_for_interrupt) starts. I 'm beginner so I dont know if it's true but it's mandatory to check that for me.

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on 31 Jan 2020, 09:34 last edited by JonB
                                  #16

                                  @Montanaro
                                  You have now been asked about 3 times whether your check_for_interrupt() is blocking or returns immediately? Are you going to answer to help those who are trying to help you, or are you going to just ignore the question?

                                  On a separate matter, it is up to you whether you want to follow the "experts'" advice you have received elsewhere, or whether you want to listen to the "experts" here as well? I do not count myself among the "experts", but there are others posting here who are experts (especially in matters Qt, which your other advisors may not be), so you might want to weigh their comments against those you have received from elsewhere.

                                  M 1 Reply Last reply 31 Jan 2020, 10:11
                                  2
                                  • J J.Hilk
                                    31 Jan 2020, 09:30

                                    @Montanaro

                                    here's the thing, multithreading /parallelization is not trivial. You'll need a good understanding of what you're doing or you end up, at best, wasting lots of time or at worst you shoot yourself in the foot.

                                    Expert suggest me to put semaphore in.

                                    I highly doubt that expert status. What is the reasoning behind that statement, please explain

                                    think I can use that:
                                    https://doc.qt.io/archives/qt-4.8/qmutexlocker.html

                                    The point of a Semaphore is not to use a mutex

                                    does check_for_interrupt actually take time at all ? And if it does how long. And what does it do exactly. There are other ways to do this, much more neater/higher level ones that mutex or semaphores especially in Qt

                                    M Offline
                                    M Offline
                                    Montanaro
                                    wrote on 31 Jan 2020, 09:43 last edited by
                                    #17

                                    @J-Hilk

                                    I dont know.
                                    He (my senior) suggests me to use that :
                                    https://doc.qt.io/archives/qt-4.8/qmutexlocker.html
                                    so I must try.

                                    I have two task:

                                    1. checking_for_interrupt. this function reads the time of flight sensor and its results are for example 120, 150, 180 (millimeter) when I dont use ui. And those results are phisically correct
                                    2. only ui shows me the number and text on my display, for example: "the distance is …. mm"

                                    but when I use together (ui and check_for_interrupt), the results of time of flight are: 3000, 3000, 3000, 3000, 3000 and it 's not possible

                                    J 1 Reply Last reply 31 Jan 2020, 09:49
                                    0
                                    • M Montanaro
                                      31 Jan 2020, 09:43

                                      @J-Hilk

                                      I dont know.
                                      He (my senior) suggests me to use that :
                                      https://doc.qt.io/archives/qt-4.8/qmutexlocker.html
                                      so I must try.

                                      I have two task:

                                      1. checking_for_interrupt. this function reads the time of flight sensor and its results are for example 120, 150, 180 (millimeter) when I dont use ui. And those results are phisically correct
                                      2. only ui shows me the number and text on my display, for example: "the distance is …. mm"

                                      but when I use together (ui and check_for_interrupt), the results of time of flight are: 3000, 3000, 3000, 3000, 3000 and it 's not possible

                                      J Offline
                                      J Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 31 Jan 2020, 09:49 last edited by
                                      #18

                                      @Montanaro Do you mean if you comment out

                                      ui->label->setText("il valore della distanza in mm è: " + QString::number(distance));
                                      

                                      in

                                      unsigned char MainWindow::poll()
                                      {
                                          unsigned short distance;
                                              qDebug() << " Polling started";
                                      
                                               check_for_interrupt(ADDR, &distance); 
                                              
                                                  qDebug() << "valore distanza:" << distance;
                                       
                                                  ui->label->setText("il valore della distanza in mm è: " + QString::number(distance));
                                      
                                          
                                              }
                                      

                                      qDebug() << "valore distanza:" << distance;
                                      prints correct values?

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

                                      1 Reply Last reply
                                      0
                                      • JonBJ JonB
                                        31 Jan 2020, 09:34

                                        @Montanaro
                                        You have now been asked about 3 times whether your check_for_interrupt() is blocking or returns immediately? Are you going to answer to help those who are trying to help you, or are you going to just ignore the question?

                                        On a separate matter, it is up to you whether you want to follow the "experts'" advice you have received elsewhere, or whether you want to listen to the "experts" here as well? I do not count myself among the "experts", but there are others posting here who are experts (especially in matters Qt, which your other advisors may not be), so you might want to weigh their comments against those you have received from elsewhere.

                                        M Offline
                                        M Offline
                                        Montanaro
                                        wrote on 31 Jan 2020, 10:11 last edited by Montanaro
                                        #19

                                        @JonB

                                        check_for_interrupt returns immediately but always the same result ( I visually dont note difference )

                                        Well… I understand your suggest but if I dont try to put mutex in, I dont accomplish my task and my senior dont countenance me :(

                                        @jsulm : no. check_for_interrupt (with ui) prints uncorrect values. it's the problem

                                        so thanks to all :)

                                        J 1 Reply Last reply 31 Jan 2020, 10:15
                                        0
                                        • M Montanaro
                                          31 Jan 2020, 10:11

                                          @JonB

                                          check_for_interrupt returns immediately but always the same result ( I visually dont note difference )

                                          Well… I understand your suggest but if I dont try to put mutex in, I dont accomplish my task and my senior dont countenance me :(

                                          @jsulm : no. check_for_interrupt (with ui) prints uncorrect values. it's the problem

                                          so thanks to all :)

                                          J Offline
                                          J Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on 31 Jan 2020, 10:15 last edited by
                                          #20

                                          @Montanaro said in Semaphore and multithread:

                                          no. check_for_interrupt (with ui) prints uncorrect values. it's the problem

                                          That's why I segested to to comment out this line

                                          ui->label->setText("il valore della distanza in mm è: " + QString::number(distance));
                                          

                                          and see what happens.
                                          Or do you mean something different if you write "with ui", because I really don't understand the problem.
                                          "when I dont use ui" - what does this mean exactly? Do you mean an app without UI works?

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

                                          M 1 Reply Last reply 31 Jan 2020, 10:33
                                          1

                                          1/25

                                          30 Jan 2020, 14:15

                                          • Login

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