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. [Solved] QMutex for public static function in multi threaded application?
Forum Updated to NodeBB v4.3 + New Features

[Solved] QMutex for public static function in multi threaded application?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.0k 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.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    I have a class as below:

    @class MyClass
    {
    private:
    static QString name;
    static QMutex *mutex;
    public:
    static void getInt(QString *result, int opt);
    }

    void MyClass::getInt(QString *result, int opt)
    {
    mutex->lock();//is this required?
    if(opt == 1)
    *result = name + " one";
    else if(opt == 2)
    *result = name + " two";
    mutex->unlock();
    }@

    So my question is that, is it required to use a QMutex for a public static const function if multiple threads may call it at the same time?
    In other words, is it necessary to synchronise a function which has read only access to class variables?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mapron
      wrote on last edited by
      #2
      1. const modifier is wrong on static function.
      2. You probably should use mutex if different threads have access to same date.
        Function that use only its parameters is thread-safe.
      3. At my point, you don't need to use mutex in you ex;
      4. You don'y modify the result param, so better type is "const QString &"
      5. pointer to QMutex is extra for me. I'd make it just an object.
      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        If your function accesses shared data, you must protect if with a mutex. If it doesn't access shared data, then you don't have to. See http://qt-project.org/doc/qt-5/threads-synchronizing.html

        Why do you use "QMutex*" instead of a "QMutex"?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • CAD_codingC Offline
          CAD_codingC Offline
          CAD_coding
          wrote on last edited by
          #4

          @mapron

          1. Thanks for your advice. I did not know that.
          2. In my case the threads have read only access to the class variables. Should I use QMutex?
          3. Updated the question, it was incorrect previously.
          4. I did for cache optimisation.

          @JKSH

          bq. If your function accesses shared data, you must protect it with a mutex.

          While the doc you linked says

          bq. When a piece of data is not being written to, it is safe for multiple threads to read from it simultaneously.

          Is it not conflicting your statement.
          BTW thanks for that link, it cleared many doubts that I had.

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            My last post was oversimplified. Please change "If your function ACCESSES shared data" to "If your function WRITES TO shared data" and "If your function READS shared data THAT MIGHT BE UPDATED".

            If your function only reads the data, and the data never changes, then you don't have to lock a mutex.

            Anyway, you're welcome; I'm glad to hear that you've learnt new things. Happy coding!

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            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