Results 1 to 4 of 4

Thread: single variable and QMutex

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default single variable and QMutex

    hi
    Is it necessary to protect a single variable with mutex when only a single variable is getting changed.

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: single variable and QMutex

    Yes, a statement such as

    Qt Code:
    1. foo++;
    To copy to clipboard, switch view to plain text mode 

    can in the object code expand to several instructions. Pseudocode:

    Qt Code:
    1. register = foo;
    2. register += 1;
    3. foo = register;
    To copy to clipboard, switch view to plain text mode 

    If another thread is also executing the same instructions, it may have been interrupted right after the first pseudo-instruction above. The variable will then be incremented only once even when two threads tried to increment it...

  3. The following user says thank you to drhex for this useful post:

    babu198649 (9th December 2008)

  4. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: single variable and QMutex

    Thanks
    Does changing a boolean value also takes more than one instruction.

  5. #4
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: single variable and QMutex

    Not if you're simply assigning true or false to it.

    But bools rarely come alone. They might, say, indicate that some data structure is ready for use (which should then also be protected by the mutex).

    If another thread needs to QWaitCondition for the bool to have a particular value, it will need a mutex argument.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.