Results 1 to 7 of 7

Thread: QMutex protection type??

  1. #1
    Join Date
    Aug 2012
    Location
    Kuala Lumpur, Malaysia
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QMutex protection type??

    Hi...
    Im doing a multi-threading program using QThread and QMutex for synchronization. my question is.. does QMutex lock all the variable in the between of lock and unlock command?? how about function that being call in the lock range?? can any other thread use the same function or data that being access by the function?? and does pointer to the lock variable also need to take turn to access the lock variable?

    sample code..
    void somefunction2(int *f);
    QMutex Dflag;

    (thread one)
    Dflag.lock;
    x=3; y=4;
    somefunction(j,k);
    Dflag.unlock;

    (thread two)
    Dflag.lock;
    x=y; y=45;
    somefunction(j,k);
    Dflag.unlock;

    (thread three)
    Dflag.lock;
    y=*x
    somefunction2(l);
    Dflag.unlock;

    thanks in advance..
    p/s: i also do write the value of the variable in .txt using QFile.. and the command is in the lock-unlock code. it is possible to have both thread to write at the same time?? i us QDateTime to keep tracking the time i get the data..

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMutex protection type??

    QMutex not protects variables, it suspends program/thread execution. So each call to your function is not preceded by an attempt to execute a semaphore lock, regardless of its condition.

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

    snow_starzz (23rd November 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMutex protection type??

    QMutex doesn't lock any variables. It makes sure no two threads have the mutex locked at the same time. If you want to protect some variable you have to make sure all calls to it are within critical sections protected by the same mutex.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    snow_starzz (23rd November 2012)

  6. #4
    Join Date
    Aug 2012
    Location
    Kuala Lumpur, Malaysia
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QMutex protection type??

    emm.. just want to make sure.. if thread one call for "somefunction" which need to access variable j.. thread two will have to wait until thread one finish execute the function due to lock command for variable j or the function right??

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMutex protection type??

    If you lock the mutex both in the function and in the other place then yes. Other than that -- no, the mutex doesn't care what is inside the critical section it protects.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    snow_starzz (27th November 2012)

  9. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMutex protection type??

    Quote Originally Posted by snow_starzz View Post
    emm.. just want to make sure.. if thread one call for "somefunction" which need to access variable j.. thread two will have to wait until thread one finish execute the function due to lock command for variable j or the function right??
    In your sample code you always call the functions from within a locked code block, so yes, only one thread can ever be executing them with the other one waiting.

    QMutex only provides you with means to make sure only one thread at a time can get past lock(). What is protected by that depends on what you as the programm do with the things accessed afterwards, i.e. you need to make sure that access to variables is always happening between lock and unlock.

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    snow_starzz (27th November 2012)

  11. #7
    Join Date
    Aug 2012
    Location
    Kuala Lumpur, Malaysia
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QMutex protection type??

    thanks all... i guess i have to revised back my program flow.. there are several thread that using the same global function in different lock's block but the lock is also different.. =(
    Last edited by snow_starzz; 27th November 2012 at 09:31.

Similar Threads

  1. Hardware ID Protection?
    By xyz247 in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2012, 05:53
  2. Replies: 3
    Last Post: 16th February 2012, 20:42
  3. QMutex QMutex::Recursive is not a type name
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2011, 15:01
  4. Replies: 1
    Last Post: 11th October 2010, 11:21
  5. program self-deactivation and protection with Qt
    By timmu in forum Qt Programming
    Replies: 3
    Last Post: 21st May 2010, 19:00

Tags for this Thread

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.