Results 1 to 10 of 10

Thread: What makes something not thread safe?

  1. #1
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default What makes something not thread safe?

    I see that alot of Qt classes are not thread safe.. What makes them not thread safe?

    Suppose I have a QPlainTextEdit box and I want to run a thread that will so some work and while its running will report back to the QPlainTextEdit box what its doing? Is this safe to do?

  2. #2
    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: What makes something not thread safe?

    Quote Originally Posted by tgreaves View Post
    I see that alot of Qt classes are not thread safe.. What makes them not thread safe?
    Static data and access to drawing resources of the underlying platform.

    Suppose I have a QPlainTextEdit box and I want to run a thread that will so some work and while its running will report back to the QPlainTextEdit box what its doing? Is this safe to do?
    If you use signals and slots or events to connect the thread and the widget then yes.

  3. #3
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What makes something not thread safe?

    Quote Originally Posted by wysota View Post
    If you use signals and slots or events to connect the thread and the widget then yes.
    What about if I pass a pointer to the widget to the thread and have it do a "widget*->setText()" there?

  4. #4
    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: What makes something not thread safe?

    No, that's a guaranteed crash sooner or later. You can't access any of the widget methods not explicitely mentioned as thread-safe from worker threads.

  5. #5
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What makes something not thread safe?

    Quote Originally Posted by wysota View Post
    If you use signals and slots or events to connect the thread and the widget then yes.
    I see that there is a appendPlainText slot.. I always connected signals and slots together to do something.. How can I "run" a slot directly from the program(or thread)?

    I know I can do widget->appendPlainText("abc") but how do I do that so its thread safe?
    Last edited by tgreaves; 20th February 2009 at 15:37.

  6. #6
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What makes something not thread safe?

    I think I might have it figured out.. Do I emit a signal in the thread and connect the signal from the thread to the plaintextedit's slot in the main program?

    --- main program
    connect(&thread, signal(addthis(QString)), ui.plainTextEdit1, slot(appendPlainText(QString));

    --- thread

    emit addthis("hello there");

    Is this correct?

  7. #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: What makes something not thread safe?

    Yes, that's fine.

  8. #8
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What makes something not thread safe?

    Great..

    And suppose there is something "funky" that I want to do using this way.. If the class(plainTextEdit, QLineEdit, ect) doesnt have a slot for that, is it ok to create my own class that inherets from (plaintextedit, qlineedit, ect) and add a slot for the "funky" stuff that I want to do.. And then connect a signal to my new classes slot, and then emit the signal for that inside of the thread?

    I dont see why I cant do that, Qt does it..

  9. #9
    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: What makes something not thread safe?

    Yes, you can subclass and add your own signals and slots. Just remember it's probably not safe to transmit pointers across threads, so avoid such "funkiness".

  10. #10
    Join Date
    Jan 2009
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What makes something not thread safe?

    Quote Originally Posted by wysota View Post
    Yes, you can subclass and add your own signals and slots. Just remember it's probably not safe to transmit pointers across threads, so avoid such "funkiness".
    Well I wouldnt be transmitting a pointer across a thread.. I would be "looking" for a signal to be emitted by the thread and running a slot inside the main program/class..

Similar Threads

  1. GUI and non-GUI thread problem
    By dimaz in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 21:25
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. Replies: 10
    Last Post: 20th March 2007, 22:19
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. Are QHttp n QHttpRequestHeader thread safe?
    By Shambhavi in forum Qt Programming
    Replies: 4
    Last Post: 21st January 2006, 08:33

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.