Results 1 to 2 of 2

Thread: Thread safe or not

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2011
    Location
    Hyderabad, India
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Thread safe or not

    Look at following code n pls let me know whether my Worker class is thread safe or not. I have a Worker class cpp as follows: m_str is Qstring member variable in Worker class n m_thread is QThread in same class.

    Qt Code:
    1. Worker::Worker() {
    2. m_thread = new QThread;
    3. moveToThread(m_thread);
    4.  
    5. connect(m_thread, SIGNAL(started()), this, SLOT(process()));
    6. connect(m_thread, SIGNAL(finished()), this, SLOT(quit()));
    7.  
    8. connect(m_thread, SIGNAL(finished()), m_thread, SLOT(deleteLater()));
    9. }
    10.  
    11. Worker::~Worker() {
    12.  
    13. }
    14.  
    15. void Worker::doStart()
    16. {
    17. .... // not touching m_str
    18. m_thread->start(); // this always last stmt. in this function
    19. }
    20.  
    21. void Worker::process() {
    22. m_str = QString("Hello!");
    23.  
    24. m_thread->quit();
    25. }
    To copy to clipboard, switch view to plain text mode 

    and in main.cpp file

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. Worker* worker = new Worker();
    6.  
    7. worker->doStart();
    8.  
    9. worker->thread()->wait();
    10.  
    11. worker->deleteLater();
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Thread safe or not

    No, it's not thread safe. No QObject based class is thread-safe and you are accessing QThread which is derived from QObject (and possibly your Worker class is also QObject derived). However your class is reentrant.
    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. Replies: 5
    Last Post: 7th February 2012, 22:13
  2. SQL module and Thread-safe
    By banita in forum Qt Programming
    Replies: 6
    Last Post: 12th August 2010, 05:30
  3. thread-safe
    By babymonsta in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2010, 10:18
  4. About the QSound,is thread safe?
    By cspp in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2009, 14:26
  5. What makes something not thread safe?
    By tgreaves in forum Newbie
    Replies: 9
    Last Post: 20th February 2009, 20:16

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.