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
Qt 6.11 is out! See what's new in the release blog

Trying to understand semaphore

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 4 Posters 5.3k 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.
  • 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
              • dheerendraD dheerendra

                @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()
                
                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on last edited by
                #21

                @dheerendra
                Sorry sir i changed and tried but the same result. But that too the second thread data i am able to display in line edit but the first thread data i am able to print using Qdebug but couldnt see the data in line edit

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

                  Can you show how did you declare semaphore ?

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

                  ManiRonM 1 Reply Last reply
                  1
                  • dheerendraD dheerendra

                    Can you show how did you declare semaphore ?

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

                    @dheerendra

                    QSemaphore addSem;

                    1 Reply Last reply
                    0
                    • dheerendraD Offline
                      dheerendraD Offline
                      dheerendra
                      Qt Champions 2022
                      wrote on last edited by
                      #24
                      1. addSem is not initialised with initial semaphore count
                      2. sleep(..) it is seconds. So to complete the loop 10 times, it takes the 10*1000 seconds. So approximately you have to wait for 3 hrs to complete this loop:). So change your sleep to sleep(1).

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

                      ManiRonM 1 Reply Last reply
                      4
                      • dheerendraD dheerendra
                        1. addSem is not initialised with initial semaphore count
                        2. sleep(..) it is seconds. So to complete the loop 10 times, it takes the 10*1000 seconds. So approximately you have to wait for 3 hrs to complete this loop:). So change your sleep to sleep(1).
                        ManiRonM Offline
                        ManiRonM Offline
                        ManiRon
                        wrote on last edited by
                        #25

                        @dheerendra
                        done sir its working

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

                          cool man. If you want the detailed example look at git

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

                          ManiRonM 1 Reply Last reply
                          1
                          • dheerendraD dheerendra

                            cool man. If you want the detailed example look at git

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

                            @dheerendra
                            Ok Sir

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

                              Now issue is resolved, you can move the issue to SOLVED state. Enjoy Qt Programming.

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

                              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