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. Trying to understand semaphore

Trying to understand semaphore

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 4 Posters 5.1k 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.
  • ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by
    #1

    I am trying to understand semaphore by doing a sample.

    but i am facing problem.

    i created two thread both perform the same operation which is addition of some data and after that the result i am passing it through a function to display it in the Qline edit . But my data is not being displayed.

    My code:

             void add_thread::run()
            {
               int b = 0;
               int c = 0;
               qDebug("ADD_");
               for (int a = 0; a < 10;++a)
                    {
                       addSem.acquire();
                          c = a + b;
                         disSem.release();
                        qDebug("ADD_DATA %d",c);
                         sleep(1000);
                        b = c;
                         emit ADD_Data(c);
                      
                       
                      }
                  qDebug("ADD_DATA %d",c);
             }
    

    Another thread Code:

            void display_thread::run()
           {
    
                int b = 0;
                int c = 0;
    
                for (int a = 10; a < 20; ++a)
                {
                   disSem.acquire();
                      c = a + b;
                   addSem.release();
                      b = c;
                       emit ADD_Data1(c);
                      sleep(1000);
    
                  }
    
    
          }
    

    Whether any mistake i Have done kindly help me is understanding it

    jsulmJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      qDebug("ADD_DATA %d",c);

      Above statement is working ? Can show us where are you connecting with ADD_Data(c) signal ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      ManiRonM 1 Reply Last reply
      0
      • ManiRonM ManiRon

        I am trying to understand semaphore by doing a sample.

        but i am facing problem.

        i created two thread both perform the same operation which is addition of some data and after that the result i am passing it through a function to display it in the Qline edit . But my data is not being displayed.

        My code:

                 void add_thread::run()
                {
                   int b = 0;
                   int c = 0;
                   qDebug("ADD_");
                   for (int a = 0; a < 10;++a)
                        {
                           addSem.acquire();
                              c = a + b;
                             disSem.release();
                            qDebug("ADD_DATA %d",c);
                             sleep(1000);
                            b = c;
                             emit ADD_Data(c);
                          
                           
                          }
                      qDebug("ADD_DATA %d",c);
                 }
        

        Another thread Code:

                void display_thread::run()
               {
        
                    int b = 0;
                    int c = 0;
        
                    for (int a = 10; a < 20; ++a)
                    {
                       disSem.acquire();
                          c = a + b;
                       addSem.release();
                          b = c;
                           emit ADD_Data1(c);
                          sleep(1000);
        
                      }
        
        
              }
        

        Whether any mistake i Have done kindly help me is understanding it

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

        @ManiRon This is not really a good example as there is no need to synchronize the threads at all. You're using local variables in each run(), so you will not have race conditions. You should find a better example where you can see what happens without synchronisation and then add synchronisation.
        Also, you're emitting signals in a loop that means slots will be executed after the loop is finished.
        Did you connect the signals to the slot?

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

        ManiRonM VRoninV 3 Replies Last reply
        0
        • jsulmJ jsulm

          @ManiRon This is not really a good example as there is no need to synchronize the threads at all. You're using local variables in each run(), so you will not have race conditions. You should find a better example where you can see what happens without synchronisation and then add synchronisation.
          Also, you're emitting signals in a loop that means slots will be executed after the loop is finished.
          Did you connect the signals to the slot?

          ManiRonM Offline
          ManiRonM Offline
          ManiRon
          wrote on last edited by
          #4

          @jsulm yes sir

          1 Reply Last reply
          0
          • dheerendraD dheerendra

            qDebug("ADD_DATA %d",c);

            Above statement is working ? Can show us where are you connecting with ADD_Data(c) signal ?

            ManiRonM Offline
            ManiRonM Offline
            ManiRon
            wrote on last edited by
            #5

            @dheerendra

             qDebug("ADD_DATA %d",c);
            

            It didnt work sir

            My MainWindow Code:

              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
            ui(new Ui::MainWindow)
             {
            ui->setupUi(this);
            
            mProducer = new add_thread(this);
            mConsumer = new display_thread(this);
            
            connect(mProducer, SIGNAL(ADD_Data(int)),
                      this, SLOT(dis_Data(int)));
            connect(mConsumer, SIGNAL(ADD_Data1(int)),
                      this, SLOT(dis_Data1(int)));
            
            
             }
            
              MainWindow::~MainWindow()
             {
                delete ui;
              }
              void MainWindow :: dis_Data(int data1)
                 {
                  //QString s = data1;
                qDebug("ADD_DATA %d",data1);
                     ui->lineEdit->setText(QString::number(data1));
                 }
                void MainWindow :: dis_Data1(int data1)
                {
                 //QString s = data1;
                qDebug("ADD_DATA %d",data1);
                      ui->lineEdit_2->setText(QString::number(data1));
                   }
            
               void MainWindow::on_pushButton_clicked()
              {
                    mProducer->start();
                      mConsumer->start();
              }
            
            1 Reply Last reply
            0
            • jsulmJ jsulm

              @ManiRon This is not really a good example as there is no need to synchronize the threads at all. You're using local variables in each run(), so you will not have race conditions. You should find a better example where you can see what happens without synchronisation and then add synchronisation.
              Also, you're emitting signals in a loop that means slots will be executed after the loop is finished.
              Did you connect the signals to the slot?

              ManiRonM Offline
              ManiRonM Offline
              ManiRon
              wrote on last edited by
              #6

              @jsulm Sir if possible can u suggest any sample that i can work out and understand semaphore better

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                Your semaphores are not same. You are acquiring one semaphore and releasing another. This is the reason your threads are locked. Do like the follows.

                                     addSem.acquire();
                                      c = a + b;
                                     addSem.release();
                

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                ManiRonM 2 Replies Last reply
                0
                • dheerendraD dheerendra

                  Your semaphores are not same. You are acquiring one semaphore and releasing another. This is the reason your threads are locked. Do like the follows.

                                       addSem.acquire();
                                        c = a + b;
                                       addSem.release();
                  
                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on last edited by
                  #8

                  @dheerendra

                  but i have two thread running the how i can make it to acquire ?

                  1 Reply Last reply
                  0
                  • dheerendraD dheerendra

                    Your semaphores are not same. You are acquiring one semaphore and releasing another. This is the reason your threads are locked. Do like the follows.

                                         addSem.acquire();
                                          c = a + b;
                                         addSem.release();
                    
                    ManiRonM Offline
                    ManiRonM Offline
                    ManiRon
                    wrote on last edited by
                    #9

                    @dheerendra

                    I tried the way u told sir, Still that problem is not solved

                    1 Reply Last reply
                    0
                    • dheerendraD Offline
                      dheerendraD Offline
                      dheerendra
                      Qt Champions 2022
                      wrote on last edited by
                      #10

                      Can you show me where are you creating the semaphore object ?

                      Dheerendra
                      @Community Service
                      Certified Qt Specialist
                      http://www.pthinks.com

                      ManiRonM 1 Reply Last reply
                      0
                      • dheerendraD dheerendra

                        Can you show me where are you creating the semaphore object ?

                        ManiRonM Offline
                        ManiRonM Offline
                        ManiRon
                        wrote on last edited by
                        #11

                        @dheerendra said in Trying to understand semaphore:

                        semaphore

                        its created seperately in a file

                         extern QSemaphore addSem;
                         extern QSemaphore disSem;
                        

                        But globally defined in MainWindow.cpp

                        1 Reply Last reply
                        0
                        • dheerendraD Offline
                          dheerendraD Offline
                          dheerendra
                          Qt Champions 2022
                          wrote on last edited by dheerendra
                          #12

                          I felt you are complicating it. Just create the QSemaphore addSem(1). You are not initialising the semaphore count. As simple stuff create the QSemaphore addSem(1) in main.cpp. Pass addSem to both the thread class.

                          Please note as @jsulm said, all this is not required. Since you would like to understand, we are showing you how to doit.

                          Another point is that - You better read how semaphores work in general. This understanding is must.

                          Dheerendra
                          @Community Service
                          Certified Qt Specialist
                          http://www.pthinks.com

                          ManiRonM 1 Reply Last reply
                          1
                          • dheerendraD dheerendra

                            I felt you are complicating it. Just create the QSemaphore addSem(1). You are not initialising the semaphore count. As simple stuff create the QSemaphore addSem(1) in main.cpp. Pass addSem to both the thread class.

                            Please note as @jsulm said, all this is not required. Since you would like to understand, we are showing you how to doit.

                            Another point is that - You better read how semaphores work in general. This understanding is must.

                            ManiRonM Offline
                            ManiRonM Offline
                            ManiRon
                            wrote on last edited by
                            #13

                            @dheerendra ok Sir

                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @ManiRon This is not really a good example as there is no need to synchronize the threads at all. You're using local variables in each run(), so you will not have race conditions. You should find a better example where you can see what happens without synchronisation and then add synchronisation.
                              Also, you're emitting signals in a loop that means slots will be executed after the loop is finished.
                              Did you connect the signals to the slot?

                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by
                              #14

                              I think you are confusing semaphores with mutexes. When you have 1 resource to protect there's no need to use a semaphore.

                              Can you tell us where do wou declare a, addSem and disSem?

                              @jsulm said in Trying to understand semaphore:

                              you're emitting signals in a loop that means slots will be executed after the loop is finished.

                              Not totally correct. If the connected slot is in a thread with an active event loop it will execute "immediately".

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              ManiRonM 2 Replies Last reply
                              3
                              • VRoninV VRonin

                                I think you are confusing semaphores with mutexes. When you have 1 resource to protect there's no need to use a semaphore.

                                Can you tell us where do wou declare a, addSem and disSem?

                                @jsulm said in Trying to understand semaphore:

                                you're emitting signals in a loop that means slots will be executed after the loop is finished.

                                Not totally correct. If the connected slot is in a thread with an active event loop it will execute "immediately".

                                ManiRonM Offline
                                ManiRonM Offline
                                ManiRon
                                wrote on last edited by
                                #15

                                @VRonin

                                declared 'a' locally and addsem and disSem globally at MainWindow.cpp

                                1 Reply Last reply
                                0
                                • VRoninV VRonin

                                  I think you are confusing semaphores with mutexes. When you have 1 resource to protect there's no need to use a semaphore.

                                  Can you tell us where do wou declare a, addSem and disSem?

                                  @jsulm said in Trying to understand semaphore:

                                  you're emitting signals in a loop that means slots will be executed after the loop is finished.

                                  Not totally correct. If the connected slot is in a thread with an active event loop it will execute "immediately".

                                  ManiRonM Offline
                                  ManiRonM Offline
                                  ManiRon
                                  wrote on last edited by
                                  #16

                                  @VRonin

                                  Actually my aim was to check how the thread runs when we use a semaphore thats why thought of having a sample which displays each value after performing the addition but it just displays the first data after that the for loop is not running

                                  1 Reply Last reply
                                  0
                                  • dheerendraD Offline
                                    dheerendraD Offline
                                    dheerendra
                                    Qt Champions 2022
                                    wrote on last edited by
                                    #17

                                    This indicates that one thread has acquired & did not release the semaphore. Show us the code with your loop for both the threads.

                                    Dheerendra
                                    @Community Service
                                    Certified Qt Specialist
                                    http://www.pthinks.com

                                    ManiRonM 2 Replies Last reply
                                    1
                                    • dheerendraD dheerendra

                                      This indicates that one thread has acquired & did not release the semaphore. Show us the code with your loop for both the threads.

                                      ManiRonM Offline
                                      ManiRonM Offline
                                      ManiRon
                                      wrote on last edited by
                                      #18

                                      @dheerendra actually i got both the values from both the thread but the value was only the first value then the next value were not updated donno whether my for loop is running?

                                      1 Reply Last reply
                                      0
                                      • dheerendraD dheerendra

                                        This indicates that one thread has acquired & did not release the semaphore. Show us the code with your loop for both the threads.

                                        ManiRonM Offline
                                        ManiRonM Offline
                                        ManiRon
                                        wrote on last edited by VRonin
                                        #19

                                        @dheerendra
                                        My thread Code:

                                        void add_thread::run()
                                        {
                                        
                                            int b = 0;
                                            int c = 0;
                                        qDebug("ADD_");
                                            for (int a = 30; a < 40;++a)
                                            {
                                                addSem.acquire();
                                                c = a + b;
                                                qDebug("ADD_DATA %d",c);
                                                disSem.release();
                                                sleep(1000);
                                                b = c;
                                                addSem.available();
                                                emit ADD_Data(c);
                                        
                                                sleep(1000);
                                            }
                                            qDebug("ADD_DATA %d",c);
                                        }
                                        
                                        
                                        My Other thread code:
                                        
                                        void display_thread::run()
                                        {
                                        
                                            int b = 0;
                                            int c = 0;
                                        
                                            for (int a = 10; a < 20; ++a)
                                            {
                                               addSem.acquire();
                                                c = a + b;
                                                addSem.release();
                                                b = c;
                                                addSem.available();
                                                emit ADD_Data1(c);
                                                sleep(1000);
                                        
                                            }
                                        
                                        
                                        }
                                        
                                        1 Reply Last reply
                                        0
                                        • dheerendraD Offline
                                          dheerendraD Offline
                                          dheerendra
                                          Qt Champions 2022
                                          wrote on last edited by dheerendra
                                          #20

                                          @ManiRon said in Trying to understand semaphore:

                                          disSem.release();

                                          My guess is right. Still you are releasing someother semaphore. Replace above line with addSem.release. Both the threads should work with same semaphore.

                                          addSem.aquire()
                                          // do something
                                          addSem.release()
                                          

                                          Dheerendra
                                          @Community Service
                                          Certified Qt Specialist
                                          http://www.pthinks.com

                                          ManiRonM 1 Reply Last reply
                                          0

                                          • Login

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