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?
Forum Updated to NodeBB v4.3 + New Features

Are local variables in static member functions thread safe?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 3 Posters 1.3k Views 1 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.
  • P Offline
    P Offline
    PowerNow
    wrote on 13 Aug 2019, 16:33 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 A 2 Replies Last reply 13 Aug 2019, 17:49
    0
    • A aha_1980
      13 Aug 2019, 20:42

      @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?!

      P Offline
      P Offline
      PowerNow
      wrote on 14 Aug 2019, 08:19 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 14 Aug 2019, 11:07
      0
      • P PowerNow
        13 Aug 2019, 16:33

        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 13 Aug 2019, 17:49 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
        • P PowerNow
          13 Aug 2019, 16:33

          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

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 13 Aug 2019, 18:24 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
          • P Offline
            P Offline
            PowerNow
            wrote on 13 Aug 2019, 20:30 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...

            A 1 Reply Last reply 13 Aug 2019, 20:42
            0
            • P PowerNow
              13 Aug 2019, 20:30

              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...

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 13 Aug 2019, 20:42 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.

              P 1 Reply Last reply 14 Aug 2019, 08:19
              2
              • A aha_1980
                13 Aug 2019, 20:42

                @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?!

                P Offline
                P Offline
                PowerNow
                wrote on 14 Aug 2019, 08:19 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 14 Aug 2019, 11:07
                0
                • P PowerNow
                  14 Aug 2019, 08:19

                  @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 14 Aug 2019, 11:07 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

                  1/7

                  13 Aug 2019, 16:33

                  • Login

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