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. Not allowing a function which is accessed by its object, to be accessed by the same object before it finishes its progress completely

Not allowing a function which is accessed by its object, to be accessed by the same object before it finishes its progress completely

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.7k 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.
  • N Offline
    N Offline
    Nouriemm
    wrote on last edited by
    #1

    Qt Geeeeeeks,

    I know what I am asking is very simple and I am not looking for a trick to solve it. What I want is a general solution.
    Here is the story:
    I have a class(classy) in my project. This class has its own files(.h and .cpp).
    I also have another cpp file; Let us call this mainWindow.cpp.
    I've got only one instance of classy in my mainwindow file (classy *obj=new classy;) But I have used obj in several different functions of it.
    Due to the fact that the mainWindow.cpp is multi-threaded, functions can run concurrently.
    It means obj starts to run a function of itself within a function of mainWindow before it finishes the same function of itself in another function of mainwindow.

    Now the Question: Is it possible to not allow a function which is accessed by its object, to be accessed by the same object before it finishes its progress completely?
    Limitations:
    1.mainWindow must be kept multi-threaded.
    2.We can not create a new instance of classy.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      I'm not sure if I understood correctly what you want to do but I think you're asking for a mutex -> http://doc.qt.io/qt-5/qmutex.html

      N 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        hi
        you mean call same function again, before last call is finished?

        N 1 Reply Last reply
        0
        • ? A Former User

          I'm not sure if I understood correctly what you want to do but I think you're asking for a mutex -> http://doc.qt.io/qt-5/qmutex.html

          N Offline
          N Offline
          Nouriemm
          wrote on last edited by
          #4

          @Wieland
          Hey there,
          I know it is not well explained but I could not explain it better than this.
          I don't think mutex can help me as far as no classy is not multi-threaded.

          kshegunovK 1 Reply Last reply
          0
          • N Nouriemm

            @Wieland
            Hey there,
            I know it is not well explained but I could not explain it better than this.
            I don't think mutex can help me as far as no classy is not multi-threaded.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @Nouriemm
            Hello, I'm not understanding the question. Could you explain what thread calls what function? Functions are in the static part of the binary (the text section) and have no knowledge of objects or threads. Two different functions can be called from different threads, notwithstanding they are methods of the same object. Also do bear in mind that Qt's GUI is non-reentrant, meaning that objects' methods are not only thread-unsafe, but instances share data as well (can't be safely pushed around different threads).

            Kind regards.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • mrjjM mrjj

              hi
              you mean call same function again, before last call is finished?

              N Offline
              N Offline
              Nouriemm
              wrote on last edited by
              #6

              @mrjj
              Not, exactly
              I want to call a function, right after the last call has finished its process completely with the same object.
              In conclusion, we have only one function and one object.
              This object appears several time in a multithread-program and calls the same function every time.
              Therefore it uses the function of its class which is already being used by itself and returns unexpected results.

              I hope that clears the topic a little bit.

              Cheers all

              ? kshegunovK 2 Replies Last reply
              0
              • N Nouriemm

                @mrjj
                Not, exactly
                I want to call a function, right after the last call has finished its process completely with the same object.
                In conclusion, we have only one function and one object.
                This object appears several time in a multithread-program and calls the same function every time.
                Therefore it uses the function of its class which is already being used by itself and returns unexpected results.

                I hope that clears the topic a little bit.

                Cheers all

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @Nouriemm Ok, now I understood. You definitely want a mutex. You should read a bit about thread-safety and reentrancy. It's a bit complicated but very important.

                N 1 Reply Last reply
                0
                • N Nouriemm

                  @mrjj
                  Not, exactly
                  I want to call a function, right after the last call has finished its process completely with the same object.
                  In conclusion, we have only one function and one object.
                  This object appears several time in a multithread-program and calls the same function every time.
                  Therefore it uses the function of its class which is already being used by itself and returns unexpected results.

                  I hope that clears the topic a little bit.

                  Cheers all

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @Nouriemm
                  Are you talking about this:

                  class A
                  {
                      void someFunction()
                      {
                          QMutexLocker locker(&someFunctionLock);
                          // ... Do other stuff
                          // This part is safely executed from multiple threads, because access to it is serialized through the mutex.
                          // BUT! If you access unprotected static/global variables here, there's no guarantee at all that this will work!
                      }
                  private:
                      QMutex someFunctionLock;
                  }
                  

                  Now, you have to know that calling someFunction() from someFunction() (even if it's indirectly invoked) will cause a deadlock.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • ? A Former User

                    @Nouriemm Ok, now I understood. You definitely want a mutex. You should read a bit about thread-safety and reentrancy. It's a bit complicated but very important.

                    N Offline
                    N Offline
                    Nouriemm
                    wrote on last edited by
                    #9

                    @Wieland
                    Interestingly, I have used mutex and it did not help (maybe I did not use it correctly).
                    Now your words are vibrating something in my head and I am going to try it again.
                    Will update the topic ASAP.

                    Cheers pal

                    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