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. Wait until condition is not met - but why mutex?
Forum Updated to NodeBB v4.3 + New Features

Wait until condition is not met - but why mutex?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qwaitcondition
14 Posts 3 Posters 1.9k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Because you want to access a shared resource and thus its access must be protected.

    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
    2
    • T Offline
      T Offline
      TomNow99
      wrote on last edited by TomNow99
      #3

      @SGaist But I know I use only once variable "a" in second Thread.

      EDIT

      I agree with you. My example is very uncommon. But why we can't have something like:

      mainThread:
      another operations
      waitCon.wait();
      mutex.lock();
      another important operations
      mutex.unlock();
      another operations
      
      second Thread:
      another operations
      mutex.lock();
      a = 6;
      mutex.unlock();
      waitCon.wakeAll();
      another operations
      

      And function wait() can be without parameter "QMutex *lockedMutex".

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

        In that case it's simply used as a mean to stop the thread execution until the wait condition is awaken.

        Depending on what you want to implement, using signals and slot might be a better tool.

        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
        • T Offline
          T Offline
          TomNow99
          wrote on last edited by
          #5

          @SGaist I can't find function like wait() when I want to use signals and slots. Of course in slot I can set variable a.

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

            Because there are none.

            Can you explain what your two threads are doing ? Especially why the first one needs to wait for a to be equal to 6 ?

            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
            0
            • T Offline
              T Offline
              TomNow99
              wrote on last edited by
              #7

              @SGaist I don't have at the moment very good example Sometimes I have code:

              fun()
              {
              fun1();
              fun2();
              fun3();
              }
              
              void timerTimeoutSlot()
              {
              do Something // for example set very important Variable;
              }
              

              I would like to something like:

              fun()
              {
              fun1();
              wait when the very important variable will be set
              fun2();
              fun3();
              }
              

              Because when this variable will not set my fun2() can't be execute.

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

                And fun should be called repeatedly ?

                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
                0
                • T Offline
                  T Offline
                  TomNow99
                  wrote on last edited by TomNow99
                  #9

                  @SGaist said in Wait until condition is not met - but why mutex?:

                  repeatedly

                  When I look at my code I think I can do something like:

                  void timerTimeoutSlot()
                  {
                  do Something // for example set very important Variable;
                  fun2();
                  fun3();
                  }
                  

                  But Could you tell me what is going on when signal is emitted? I talk about only 1 thread. For example:

                  function()
                  {
                  operation a;
                  operation b;
                  operation c;
                  operation d;
                  operation e;
                  operation f;
                  }
                  
                  timerTimeoutSlot()
                  {
                  operation m;
                  operation n;
                  operation o;
                  }
                  
                  timer2TimeoutSlot()
                  {
                  operation x;
                  operation y;
                  operation z;
                  }
                  

                  And when the operation c is execute, signal timeout in timer1 is emitted
                  So operations will be execute like:
                  a -> b -> c -> m -> n -> o -> d -> e -> f?

                  And second question:
                  When the operation c is execute, signal timeout in timer1 is emitted and when operation m is execute, signal timeout in timer 2 is emitted. So operation will be execute like:
                  a -> b -> c -> m -> x->y->z->n -> o -> d -> e -> f?

                  EDIT:
                  I hear that can be something like:
                  a -> b -> c -> m -> d->n->e->o->f ( in first question )

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

                    If single threaded, the slots will be called after the functions are done in order.

                    Looks like you are trying to model some sort of behaviour. What would that be ?

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

                    Q 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      If single threaded, the slots will be called after the functions are done in order.

                      Looks like you are trying to model some sort of behaviour. What would that be ?

                      Q Offline
                      Q Offline
                      qwe3
                      wrote on last edited by
                      #11

                      @SGaist so will be a b c d e f m n o
                      Or
                      A b c m d n e o f?

                      I am a newbie programmer, so my questions are not very smart and I dont have any plans.

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

                        Your answers makes it look like you are trying to model some behaviour that could be represented by a state machine rather that complex threading.

                        Since you are starting in the programming field, you should rather stay away from the threading paradigm and learn the rest first. Threading is a very complex topic that offers you many way to shoot yourself in the foot from different unrelated angles at different unrelated time with different unrelated side-effects that will want to go after your foot again.

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

                        Q 1 Reply Last reply
                        2
                        • SGaistS SGaist

                          Your answers makes it look like you are trying to model some behaviour that could be represented by a state machine rather that complex threading.

                          Since you are starting in the programming field, you should rather stay away from the threading paradigm and learn the rest first. Threading is a very complex topic that offers you many way to shoot yourself in the foot from different unrelated angles at different unrelated time with different unrelated side-effects that will want to go after your foot again.

                          Q Offline
                          Q Offline
                          qwe3
                          wrote on last edited by
                          #13

                          @SGaist Ok :) I will learn thread after easier thing. But please answer for my question about operations. Thank you

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

                            If timer2 timeout value is greater than timer they will be in order: function -> timerTimeoutSlot -> timer2TimeoutSlot.

                            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
                            0

                            • Login

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