Results 1 to 9 of 9

Thread: Why slots in QThread subclasses are unsafe?

  1. #1
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Why slots in QThread subclasses are unsafe?

    Im am refering to this statement from the QThread documentation page:
    Like other objects, QThread objects live in the thread where the object was created -- not in the thread that is created when QThread::run() is called. It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex.
    For example I want to tell the thread to do something while it's running. So I create a slot or a standard method and the slot sets a variable which the Qthread::run method reads and then does something. Do I have to use a mutex even if just one thread writes the variable and one only reads it?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Why slots in QThread subclasses are unsafe?

    It's much safer to add your slots to another object which lives inside the thread:
    Qt Code:
    1. MyObjectWithFancySlots obj;
    2. QThread thread;
    3. obj.moveToThread(&thread);
    4. thread.start();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    AlphaWolf (19th April 2009)

  4. #3
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why slots in QThread subclasses are unsafe?

    Ahh that's cool

    Edit: When I call a method of this object will it be executed inside the calling thread or inside the object's thread?
    Last edited by AlphaWolf; 19th April 2009 at 18:01.

  5. #4
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why slots in QThread subclasses are unsafe?

    When I call a method of this object will it be executed inside the calling thread or inside the object's thread?

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Why slots in QThread subclasses are unsafe?

    If you call it directly like a normal C++ function, it will be executed in the callers thread of course. You can use signals and slots, custom events or QMetaObject::invokeMethod() get it executed in the worker thread context.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    AlphaWolf (19th April 2009)

  8. #6
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why slots in QThread subclasses are unsafe?

    ok that's helpful

  9. #7
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why slots in QThread subclasses are unsafe?

    Sorry for the double posts but I have another question:

    I want the second thread to render into a widget using DirectX all the time to have a smooth scene.

    So I have a loop which runs from the beginning till the end of my application. The problem is that my rendering thread needs a message loop in order to execute QMetaObject::invokeMethod() or use signal and slots. You see my problem? When I call QThread::exec() I can't use my loop because Qthread:exec() is blocky by design.

  10. #8
    Join Date
    May 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Why slots in QThread subclasses are unsafe?

    Quote Originally Posted by AlphaWolf View Post
    So I have a loop which runs from the beginning till the end of my application. The problem is that my rendering thread needs a message loop in order to execute QMetaObject::invokeMethod() or use signal and slots. You see my problem? When I call QThread::exec() I can't use my loop because Qthread:exec() is blocky by design.
    This is exactly the problem I am facing now - I need to use QUdpSocket, which requires an event loop in order to trigger the readyRead signal, AND I want to have essentially an endless loop such that I can process other things as I need to. (FYI, thread is here.) I think the only solution is to move functionality to another class, possibly another thread altogether. Preferably, I'd like to keep the thread count as low as possible as I want to avoid introding problems that may be harder to pinpoint due to the multithreaded approach. How did you resolve your problem in the end?

    Cheers,

    Xav.

  11. #9
    Join Date
    May 2010
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why slots in QThread subclasses are unsafe?

    Quote Originally Posted by xavierda View Post
    How did you resolve your problem in the end?
    Actually, just worked this out; my solution is here. See what you think!

    Cheers,

    Xav.

Similar Threads

  1. QTableWidget, QThread, signals and slots
    By kazek3018 in forum Newbie
    Replies: 4
    Last Post: 30th December 2008, 21:21
  2. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 01:47

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.