Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Are local variables in static member functions thread safe?

Are local variables in static member functions thread safe?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 3 Posters 1.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.
  • PowerNowP Offline
    PowerNowP Offline
    PowerNow
    wrote on last edited by PowerNow
    #1

    Hi,
    here the following shema.

    class {
    ...
    static funct1() {
         uses local variables and Qt functions;
         funct2();
    }
    
    static funct2() {
         uses local variables & Qt functions;
    }
    
    funct3() {
    ....
    QFuture<bool> qf = QtConcurrent::mapped(array, funct1);
    }
    }
    

    The code works correctly, but I'm still not sure if I have to make funct2() thread safe? As long I'm not using global and static varibles it should be correct. But what's also about the Qt functions?

    Thxs

    K aha_1980A 2 Replies Last reply
    0
    • aha_1980A aha_1980

      @powernow said in Are local variables in static member functions thread safe?:

      Are local variables in a static member function stored in a separate stack for each call from a thread like for local variables in normal member functions?

      Yes.

      If yes then also komplexe objects should be thread safe?

      As I said, not necessarily.

      Were do I check if a QT function ist thread safe?

      In the documentation?!

      PowerNowP Offline
      PowerNowP Offline
      PowerNow
      wrote on last edited by
      #6

      @aha_1980 "Many Qt classes are reentrant, but they are not made thread-safe", so if I not use shared data (reentrant) in static member functions it works. The main point is that threads also for static member functions uses their own stack. Thxs!

      K 1 Reply Last reply
      0
      • PowerNowP PowerNow

        Hi,
        here the following shema.

        class {
        ...
        static funct1() {
             uses local variables and Qt functions;
             funct2();
        }
        
        static funct2() {
             uses local variables & Qt functions;
        }
        
        funct3() {
        ....
        QFuture<bool> qf = QtConcurrent::mapped(array, funct1);
        }
        }
        

        The code works correctly, but I'm still not sure if I have to make funct2() thread safe? As long I'm not using global and static varibles it should be correct. But what's also about the Qt functions?

        Thxs

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #2

        @powernow

        Probably you have to check with the functions you are using. It should be in the documentation of the functions you are using. At least I have stumbled a couple of times over the thread savety declaration in the documents. I doubt that anybody can give otherwise a general statement for all functions.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        1
        • PowerNowP PowerNow

          Hi,
          here the following shema.

          class {
          ...
          static funct1() {
               uses local variables and Qt functions;
               funct2();
          }
          
          static funct2() {
               uses local variables & Qt functions;
          }
          
          funct3() {
          ....
          QFuture<bool> qf = QtConcurrent::mapped(array, funct1);
          }
          }
          

          The code works correctly, but I'm still not sure if I have to make funct2() thread safe? As long I'm not using global and static varibles it should be correct. But what's also about the Qt functions?

          Thxs

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @powernow

          Local variables of POD (plain old data) types, like int, bool, float are most likely thread-safe.

          If you have classes as local variables, then it depends if these access other global memory.

          As @koahnig already said, it will be hard to give a general advice without a specific example.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          0
          • PowerNowP Offline
            PowerNowP Offline
            PowerNow
            wrote on last edited by
            #4

            Are local variables in a static member function stored in a separate stack for each call from a thread like for local variables in normal member functions? If yes then also komplexe objects should be thread safe? Were do I check if a QT function ist thread safe? Thxs...

            aha_1980A 1 Reply Last reply
            0
            • PowerNowP PowerNow

              Are local variables in a static member function stored in a separate stack for each call from a thread like for local variables in normal member functions? If yes then also komplexe objects should be thread safe? Were do I check if a QT function ist thread safe? Thxs...

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @powernow said in Are local variables in static member functions thread safe?:

              Are local variables in a static member function stored in a separate stack for each call from a thread like for local variables in normal member functions?

              Yes.

              If yes then also komplexe objects should be thread safe?

              As I said, not necessarily.

              Were do I check if a QT function ist thread safe?

              In the documentation?!

              Qt has to stay free or it will die.

              PowerNowP 1 Reply Last reply
              2
              • aha_1980A aha_1980

                @powernow said in Are local variables in static member functions thread safe?:

                Are local variables in a static member function stored in a separate stack for each call from a thread like for local variables in normal member functions?

                Yes.

                If yes then also komplexe objects should be thread safe?

                As I said, not necessarily.

                Were do I check if a QT function ist thread safe?

                In the documentation?!

                PowerNowP Offline
                PowerNowP Offline
                PowerNow
                wrote on last edited by
                #6

                @aha_1980 "Many Qt classes are reentrant, but they are not made thread-safe", so if I not use shared data (reentrant) in static member functions it works. The main point is that threads also for static member functions uses their own stack. Thxs!

                K 1 Reply Last reply
                0
                • PowerNowP PowerNow

                  @aha_1980 "Many Qt classes are reentrant, but they are not made thread-safe", so if I not use shared data (reentrant) in static member functions it works. The main point is that threads also for static member functions uses their own stack. Thxs!

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #7

                  @powernow said in Are local variables in static member functions thread safe?:

                  @aha_1980 "Many Qt classes are reentrant, but they are not made thread-safe", so if I not use shared data (reentrant) in static member functions it works. The main point is that threads also for static member functions uses their own stack. Thxs!

                  That is the Qt definition you can rely on https://doc.qt.io/qt-5/threads-reentrancy.html

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  3

                  • Login

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