Results 1 to 7 of 7

Thread: Public functions in a QThread class: safe?

  1. #1
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Public functions in a QThread class: safe?

    The question I am about to ask was actually asked in this thread: http://www.qtforum.org/article/32113...questions.html

    But wasn't really properly answered...

    Here is some code to explain the question:

    Subclassed from QThread:
    Qt Code:
    1. thread::run()
    2. {
    3. while(1)
    4. {
    5. if(someCondition)
    6. privateMember++;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. thread::getPrivateMember()
    2. {
    3. return privateMember;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Q: Is it necessary to protect privateMember with mutexes in the above case [as in a lock()/unlock() pair in run() and a QMutexLocker in getPrivateMember()] ?

    OR, is privateMember automatically protected seeing as the "get" function is a member function of a QThread-derived class. In other words, run() and getPrivateMember() will never run at the same time and are thus protected?

    Can someone shed some light on this?

    Cheers

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Public functions in a QThread class: safe?

    1. It's not safe, since objects with different thread affinity can call getPrivateMember().

    2. You should use a wait condition instead of busy looping.
    QWaitCondition

    3. I would recommend that you instead subclass QObject, create a QThread instance and use moveToThread() to set the thread affinity of your object, as mentioned in this article:

    http://labs.qt.nokia.com/2010/06/17/...oing-it-wrong/

    With this approach + using signal and slot connections you can avoid a lot of these issues.

    Usually, this means simply changing your class to inherit from QObject instead of QThread and, possibly, changing the class name. QThread has a started() signal that you can connect to when you need to perform some initialization. To actually have your code run in the new thread context, you need to instantiate a QThread and assign your object to that thread using the moveToThread() function.

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

    dmginc (31st January 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Public functions in a QThread class: safe?

    Q: Is it necessary to protect privateMember with mutexes in the above case [as in a lock()/unlock() pair in run() and a QMutexLocker in getPrivateMember()] ?
    There is a simple rule to it:
    If the variable can be accessed by at least two threads at the same time, it has to be guarded by a mutex, or semaphore or similar mutual exclusion mechanism.
    This has nothing to do with variable access privileges (public/protected/private).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    dmginc (31st January 2011)

  6. #4
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Public functions in a QThread class: safe?

    On second thought, a wait condition is perhaps not what you need in this case. But busy looping is still a bad idea, and you can still use above mentioned approach taking advantage of Qts signal/slots mechanism instead.

  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: Public functions in a QThread class: safe?

    If privateMember is of type QAtomicInt, it doesn't have to be protected. If it is a regular int, all what has already been said here applies.
    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:

    dmginc (31st January 2011)

  9. #6
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Public functions in a QThread class: safe?

    Quote Originally Posted by wysota View Post
    If privateMember is of type QAtomicInt, it doesn't have to be protected. If it is a regular int, all what has already been said here applies.
    Thanks for that. This is the answer to the question I wanted to know

    Quote Originally Posted by helloworld View Post
    On second thought, a wait condition is perhaps not what you need in this case. But busy looping is still a bad idea, and you can still use above mentioned approach taking advantage of Qts signal/slots mechanism instead.
    Perhaps the "if" condition in my example code wasn't the best choice. I merely wanted to show a private member of the class being altered in run().

    Quote Originally Posted by helloworld View Post
    1. It's not safe, since objects with different thread affinity can call getPrivateMember().
    So basically, when I call the "get" public function from another thread, it is executed at the same time as run() (that's what I wasn't sure of..)? In other words, run() does not block until the "get" function returns?

    Thanks again.

  10. #7
    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: Public functions in a QThread class: safe?

    Quote Originally Posted by dmginc View Post
    So basically, when I call the "get" public function from another thread, it is executed at the same time as run() (that's what I wasn't sure of..)?
    Not necessarily but there is no guarantee that it doesn't.

    In other words, run() does not block until the "get" function returns?
    It's just a function call like any other. Why would anything block? Especially that the QThread object lives in the same thread as the method calling it.
    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.


Similar Threads

  1. class calling other class functions?
    By Hardstyle in forum Newbie
    Replies: 4
    Last Post: 2nd June 2010, 02:38
  2. Replies: 7
    Last Post: 15th January 2010, 20:45
  3. default on class' public data
    By baray98 in forum General Programming
    Replies: 2
    Last Post: 29th July 2008, 02:04
  4. Replies: 5
    Last Post: 20th April 2008, 20:30
  5. DLL exporting functions, variables, class
    By Shuchi Agrawal in forum Newbie
    Replies: 1
    Last Post: 25th April 2007, 11:40

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.