Are local variables in static member functions thread safe?



  • 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



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


  • Qt Champions 2018

    @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



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


  • Qt Champions 2018

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



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



  • @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.