Results 1 to 18 of 18

Thread: How can i get notification obout thread deleting ?

  1. #1
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default How can i get notification obout thread deleting ?

    I wrote a code of creation thread like this example http://mayaposch.wordpress.com/2011/...l-explanation/
    I only added two signal/slot connections:
    connect(clientthread, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCT(QObject* )));
    connect(CT, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCST(QObject* )));
    But they doesn't work. Could anybody advice me, why those connections doesn't work and i can't get notification of deletion of objects ?
    code below:
    Qt Code:
    1. // ================= clientsocket.h ==================
    2. class ClientSocketThread : public QObject
    3. {
    4. Q_OBJECT
    5. public:
    6. ClientSocketThread(QObject * parent = 0);
    7. ~ClientSocketThread();
    8. private:
    9. volatile bool stopped;
    10. QObject * MW;
    11.  
    12. public slots:
    13. void process();
    14. void stop();
    15.  
    16. signals:
    17. void finished();
    18.  
    19. };
    20. // =========== mainwindow.cpp ======================
    21. void MainWindow::CreateClientThread()
    22. {
    23. isDeleted = 0;
    24. CT = new ClientSocketThread(this);
    25. clientthread = new QThread(this);
    26. CT->moveToThread(clientthread);
    27.  
    28. connect(clientthread, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCT(QObject* )));
    29. connect(CT, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCST(QObject* )));
    30.  
    31. connect(clientthread, SIGNAL(started()), CT, SLOT(process()));
    32. connect(CT, SIGNAL(finished()), clientthread, SLOT(quit()));
    33. connect(CT, SIGNAL(finished()), CT, SLOT(deleteLater()));
    34. connect(clientthread, SIGNAL(finished()), clientthread, SLOT(deleteLater()));
    35.  
    36. clientthread->start();
    37. }
    38.  
    39. void MainWindow::DeleteClientThread()
    40. {
    41. CT->stop();
    42. while(!(isDeleted == 2));
    43. }
    44.  
    45. void MainWindow::onDestroyCT(QObject* obj)
    46. {
    47. isDeleted++;
    48. }
    49.  
    50. void MainWindow::onDestroyCST(QObject* obj)
    51. {
    52. isDeleted++;
    53. }
    54. // ========================= clientsocket.cpp ===========================
    55. ClientSocketThread::ClientSocketThread(QObject * parent)
    56. {
    57. MW = parent;
    58. stopped = false;
    59. ready = false;
    60. }
    61.  
    62. ClientSocketThread::~ClientSocketThread()
    63. {
    64. //
    65. }
    66.  
    67.  
    68. void ClientSocketThread::stop()
    69. {
    70. stopped = true;
    71. }
    72.  
    73. void ClientSocketThread::update()
    74. {
    75. ready = false;
    76. }
    77.  
    78. void ClientSocketThread::_run()
    79. {
    80. while (!stopped)
    81. {
    82.  
    83. }
    84. }
    85.  
    86. void ClientSocketThread::process()
    87. {
    88. QString infostr = "hello !!!";
    89. while (!stopped)
    90. {
    91. while (!stopped);
    92. QCoreApplication::postEvent(MW,new TS_SocketEvent(SocketEventDebug,infostr));
    93. }
    94. emit finished();
    95. }
    To copy to clipboard, switch view to plain text mode 

  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: How can i get notification obout thread deleting ?

    Because loop while(!(isDeleted == 2)) blocks event loop.

  3. #3
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How can i get notification obout thread deleting ?

    Thanks,that sounds right, because this method is called by button's clicking. But How can I unblock eventloop and create a new event for button's clicking (at the end of queue of events) or pass another events while "while(!(isDeleted == 2))" is waiting in order to process destroyed() signal ? Of caurse, I can create special thread for this case but i don't think that is good idea. May be, Do you advice me an own approach for this issue ?
    Last edited by Pechkin; 14th March 2014 at 16:09.

  4. #4
    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: How can i get notification obout thread deleting ?

    Just don't execute this loop.
    If you want to do anything when isDeleted reaches 2 then do that in the slots where it is incremented

    Cheers,
    _

  5. #5
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How can i get notification obout thread deleting ?

    Quote Originally Posted by anda_skoa View Post
    Just don't execute this loop.
    If you want to do anything when isDeleted reaches 2 then do that in the slots where it is incremented

    Cheers,
    _
    The Aim is execution of complete action of deletion of thread in button's click event handler. Of caurse i can temporary make inactive button to a call of slot of destroeyed() signal but i'm not sure that is good idea. maybe if i call processEvents() in button click event handler i'll reach my aim but i'm not sure.
    Last edited by Pechkin; 15th March 2014 at 19:39.

  6. #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: How can i get notification obout thread deleting ?

    If you call processEvents that is essentially the same as not doing the loop.

    Why do you need to block the UI until the thread is deleted?

    Cheers,
    _

  7. #7
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How can i get notification obout thread deleting ?

    Quote Originally Posted by anda_skoa View Post
    If you call processEvents that is essentially the same as not doing the loop.

    Why do you need to block the UI until the thread is deleted?

    Cheers,
    _
    In order to restore order. Otherwise the process of creation threads theoretically may outrun the process of deletion object and as conclusion
    1)the overflow of queue of events or
    2)overflow of heap because of many stubs of non-completly deleted threads
    What situation more possible depend on internal architecture qt.

  8. #8
    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: How can i get notification obout thread deleting ?

    You might want to look into reusing the thread or using a thread pool.

    Anyway, how about showing a modal progress dialog?

    Prohibits user interaction with the main UI, continues to run event processing, makes it clear to the user that the program is waiting for seomthing, can be made to not appear if the thread ends quickly enough.

    Cheers,
    _

  9. #9
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How can i get notification obout thread deleting ?

    I've stopped on this solving:
    Qt Code:
    1. // mainwindow.h ====================================================================
    2. void MainWindow::on_pushButton_Create_clicked()
    3. {
    4. pth = new TestThread(this);
    5. clientthread = new QThread(this);
    6. pth->moveToThread(clientthread);
    7. //connect(clientthread, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCT(QObject* )));
    8. //connect(CT, SIGNAL(destroyed(QObject* )), this, SLOT(onDestroyCST(QObject* )));
    9. connect(clientthread, SIGNAL(started()), pth, SLOT(process()));
    10. //connect(CT, SIGNAL(finished()), clientthread, SLOT(quit()));
    11. //connect(CT, SIGNAL(finished()), CT, SLOT(deleteLater()));
    12. //connect(clientthread, SIGNAL(finished()), clientthread, SLOT(deleteLater()));
    13. clientthread->start();
    14. ui->pushButton_Create->setDisabled(true);
    15. ui->pushButton_Delete->setEnabled(true);
    16. }
    17.  
    18. void MainWindow::on_pushButton_Delete_clicked()
    19. {
    20. pth->stop();
    21. while(!pth->isStopped);
    22. clientthread->terminate();
    23. //while(!clientthread->isRunning()); // why doesn't work ???
    24. delete pth;
    25. delete clientthread;
    26. ui->pushButton_Create->setEnabled(true);
    27. ui->pushButton_Delete->setDisabled(true);
    28. }
    29. // thread.h ===========================================================================
    30. class TestThread : public QObject
    31. {
    32. Q_OBJECT
    33. public:
    34. explicit TestThread(QObject * parent);
    35. volatile bool isStopped;
    36.  
    37. private:
    38. volatile bool isStopping;
    39. QObject * MW;
    40. public slots:
    41. void process();
    42. void stop();
    43. };
    44. // thread.cpp =====================================================================
    45. TestThread::TestThread(QObject * parent) : MW(parent)
    46. {
    47. isStopped = false;
    48. isStopping = false;
    49. }
    50.  
    51. void TestThread::stop()
    52. {
    53. isStopping = true;
    54. }
    55.  
    56. void TestThread::process()
    57. {
    58. QString infostr = "hello !!!";
    59. while (!isStopping)
    60. {
    61. while (!isStopping);
    62. QCoreApplication::postEvent(MW,new TestEvent(TestEventDebug,""));
    63. }
    64. isStopped = true;
    65. }
    To copy to clipboard, switch view to plain text mode 
    It seems problem is solved but why doesn't work "while(!clientthread->isRunning());" ???

  10. #10
    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: How can i get notification obout thread deleting ?

    You are accessing two variables from two threads without protecting them.

    Do not call terminate().
    Call quit().

    join() the thread to wait for its exit.

    Bllocking the UI is usually considered a very bad thing.

    Cheers,
    _

  11. #11
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How can i get notification obout thread deleting ?

    Quote Originally Posted by anda_skoa View Post
    You are accessing two variables from two threads without protecting them.
    _
    Qt have properties's construction like Delphi, but i haven't learnt how use it yet. I corrected code a little bit:
    Qt Code:
    1. class TestThread : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit TestThread(QObject * parent);
    6. ~TestThread();
    7. QEventLoop * ploop;
    8. bool GetStopState() { return isStopped; } // <---------- property
    9. private:
    10. volatile bool isStopping;
    11. volatile bool isStopped;
    12. volatile bool ready;
    13. QObject * MW;
    14. uint timerid;
    15. QTimer * ptimer;
    16. protected:
    17. void timerEvent(QTimerEvent *event);
    18. public slots:
    19. void process();
    20. void stop();
    21. void update();
    22. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::on_pushButton_Delete_clicked()
    2. {
    3. pth->stop();
    4. while(!pth->GetStopState());
    5. clientthread->quit();
    6. //while(!clientthread->isRunning());
    7. delete pth;
    8. delete clientthread;
    9. ui->pushButton_Create->setEnabled(true);
    10. ui->pushButton_Delete->setDisabled(true);
    11. }
    To copy to clipboard, switch view to plain text mode 
    For the rest i haven't found a problem because for each variable write access from one thread and read access from one thread.

    Quote Originally Posted by anda_skoa View Post
    Do not call terminate().
    Call quit().
    _
    Thank's Really it's true;

    Quote Originally Posted by anda_skoa View Post
    You are accessing two variables from two threads without protecting them.
    join() the thread to wait for its exit.
    Cheers,
    _
    Excuse me. I haven't caught this. What is "join()" ?
    Quote Originally Posted by anda_skoa View Post
    Bllocking the UI is usually considered a very bad thing.
    Cheers,
    _
    I agree
    Last edited by Pechkin; 16th March 2014 at 16:09.

  12. #12
    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: How can i get notification obout thread deleting ?

    Protecting access
    Qt Code:
    1. class Test : public QObject
    2. {
    3. public:
    4. Test() : m_stop(false), m_isStopped(false) {}
    5.  
    6. void stop()
    7. {
    8. QMutexLocker locker(&m_mutex);
    9. m_stop = true;
    10. }
    11.  
    12. bool isStopped() const
    13. {
    14. QMutexLocker locker(&m_mutex);
    15. return m_isStopped;
    16. }
    17.  
    18. private:
    19. bool shouldStop() const
    20. {
    21. QMutexLocker locker(&m_mutex);
    22. return m_stop;
    23. }
    24.  
    25. void hasStopped()
    26. {
    27. QMutexLocker locker(&m_mutex);
    28. m_isStopped = true;
    29. }
    30.  
    31. void process()
    32. {
    33. while (!shouldStop()) {
    34. }
    35. hasStopped();
    36. }
    37.  
    38. private:
    39. mutable QMutex;
    40. };
    To copy to clipboard, switch view to plain text mode 

    And with join() I was referring to QThread::join()

    Cheers,
    _

  13. #13
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How can i get notification obout thread deleting ?

    Quote Originally Posted by anda_skoa View Post
    Protecting access
    Qt Code:
    1. class Test : public QObject
    2. {
    3. public:
    4. Test() : m_stop(false), m_isStopped(false) {}
    5.  
    6. void stop()
    7. {
    8. QMutexLocker locker(&m_mutex);
    9. m_stop = true;
    10. }
    11.  
    12. bool isStopped() const
    13. {
    14. QMutexLocker locker(&m_mutex);
    15. return m_isStopped;
    16. }
    17.  
    18. private:
    19. bool shouldStop() const
    20. {
    21. QMutexLocker locker(&m_mutex);
    22. return m_stop;
    23. }
    24.  
    25. void hasStopped()
    26. {
    27. QMutexLocker locker(&m_mutex);
    28. m_isStopped = true;
    29. }
    30.  
    31. void process()
    32. {
    33. while (!shouldStop()) {
    34. }
    35. hasStopped();
    36. }
    37.  
    38. private:
    39. mutable QMutex;
    40. };
    To copy to clipboard, switch view to plain text mode 

    And with join() I was referring to QThread::join()

    Cheers,
    _
    Thank's It's excelent example. But, I can't understand, What reason of use of mutex ? What risk it protects from ? Especially in constant methods.
    I haven't found join() method in QThread class documentation (QT 5.2).
    Last edited by Pechkin; 17th March 2014 at 12:41.

  14. #14
    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: How can i get notification obout thread deleting ?

    Quote Originally Posted by Pechkin View Post
    Thank's It's excelent example. But, I can't understand, What reason of use of mutex ?
    The mutex ensures that only one thread can access the protected variables at the same time.

    Quote Originally Posted by Pechkin View Post
    What risk it protects from ?
    Aside from it being very sloppy to have code that depends on chance, it ensures correct value propagation and value consistency.

    Quote Originally Posted by Pechkin View Post
    Especially in constant methods.
    Whether or not a method is constant doesn't matter. I just made the "readers" const because that is common practise.

    Quote Originally Posted by Pechkin View Post
    I haven't found join() method in QThread class documentation (QT 5.2).
    Sorry, my bad, it is called wait().
    It is called join() in C++11's thread and I mixed that up

    Cheers,
    _

  15. #15
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How can i get notification obout thread deleting ?

    Quote Originally Posted by anda_skoa View Post
    The mutex ensures that only one thread can access the protected variables at the same time.
    Aside from it being very sloppy to have code that depends on chance, it ensures correct value propagation and value consistency.
    Whether or not a method is constant doesn't matter. I just made the "readers" const because that is common practise.
    Cheers,
    _
    Thank's, I know why variables need mutex.
    May be, generally, the using of protected by mutex variables, is good style of programming, but what reason does use it here ? My target is program, which should compiles with qt 3.x and works under 16 MB of RAM & 200MhZ of CPU so i try to avoid using unnecessary code . If doesn't exist real risk of conflict access in current situation i'll prefer don't use it.
    Last edited by Pechkin; 17th March 2014 at 14:36.
    Best regards.

  16. #16
    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: How can i get notification obout thread deleting ?

    As I said, the mutex is about guaranteeing certain things. If you can live with uncertainty then this is your choice.

    My assumption was that you wanted to be sure the values are propagated correctly since you are blocking your UI.

    Undefined behavior can be really nasty to debug.

    Cheers,
    _

  17. #17
    Join Date
    Mar 2014
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How can i get notification obout thread deleting ?

    Quote Originally Posted by anda_skoa View Post
    As I said, the mutex is about guaranteeing certain things. If you can live with uncertainty then this is your choice.

    My assumption was that you wanted to be sure the values are propagated correctly since you are blocking your UI.

    Undefined behavior can be really nasty to debug.

    Cheers,
    _
    You are right.
    My program uses 2 threads: main thread and this thread( thread2). m_stop has "write only" access from main thread and "read only" access from thread2.
    m_isStopped has "read only access" from main thread and "write only" access from thread2. i'll want to be sure that undefined behavior is impossible if i don't use mutex. I'll be appreciated if you explain me why case of UB is real for current case.
    Last edited by Pechkin; 17th March 2014 at 16:03.
    Best regards.

  18. #18
    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: How can i get notification obout thread deleting ?

    In your specific case you could be lucky, because the two writes are in a definite order, each true value is only read once and the value after that doesn't matter anymore.

    In general you don't have to nice coinicdences. Each write will result in a memory write of an area that is as wide as the bus between CPU and memory. To alter only the one byte of the boolean (assuming the compiler doesn't even do more packing), the CPU will need to read the word, change one the bytes and then write the word back.
    You do not want it to write anything else into the remaining bytes just because it is now executing the write of a different thread.

    On top of that, in a system with multiple CPUs, you would probably like to ensure that the changed value is actually written to the memory and not just in the cache of the CPU executing the writing thread.

    See https://en.wikipedia.org/wiki/Memory_barrier

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 30th December 2010, 21:38
  2. Deleting a thread content
    By Luc4 in forum Qt Programming
    Replies: 17
    Last Post: 21st April 2010, 08:39
  3. Notification in KDE
    By mpele in forum KDE Forum
    Replies: 0
    Last Post: 23rd November 2009, 10:24
  4. Replies: 2
    Last Post: 2nd August 2008, 10:16
  5. Replies: 1
    Last Post: 2nd August 2008, 10:05

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.