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. Does QMutex lock all the code between two lock call?

Does QMutex lock all the code between two lock call?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.1k 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.
  • R Offline
    R Offline
    rspock
    wrote on last edited by
    #1

    Suppouse I have this class
    @
    class Foo {
    public:
    int getValue(){
    QMutexLocker(&mutex);
    return this->value;
    }

    public slots:
    void updateValue(){
    QMutexLocker(&mutex);
    //doing stuff
    this->value = someValue;
    }

    private:
    int value;
    QMutex mutex;
    }
    @

    Am I sure that any possbile thread is waiting in getValue() while some other thread is running updateValue()?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Yup. Do you have some concerns about it?

      One comment I have, although not necessarily applicable here, is that the "area" locked by mutex should be minimized e.g. if in your example "doing stuff" is some local computation that doesn't access any shared data then you could move it above the lock and only guard the final writing the result to the shared variable (value). Locking too much voids the benefits of multiple threads because locking a mutex is basically serializing execution and making threads wait.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rspock
        wrote on last edited by
        #3

        Thank you!

        This is achieved because the mutex is a class member, isn't it? Or, even better ,because I use the same mutex instance for both method?.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          It doesn't matter if it's a member or not. It would be the same if you had a global mutex. The important part is that the same mutex guards both regions.

          Remember though that if you have a member mutex it will guard only access to that single instance of your object. If you want to separate calls to getValue and updateValue of any instance (e.g. when they use some shared globals or statics) then you need to use a global or static mutex, so that all access is guarded by the same one.

          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