Results 1 to 2 of 2

Thread: qtconcurrent dont close correctly

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default qtconcurrent dont close correctly

    hi everybody, i want to create a unkillable process so i m using a mq pattern run in different thread like this

    Qt Code:
    1. void UnkillApp::checkKeepServer()
    2. {
    3. const char* recm = s_recv(socket);
    4. if(recm == NULL)
    5. {
    6. QProcess::startDetached(m_cpath);
    7. }
    8. else if(strcasecmp(recm, "kill") == 0)
    9. {
    10. s_send(socket, "kill");
    11.  
    12. p.setProgram(m_cpath);
    13. p.kill();
    14. }
    15.  
    16. s_send(socket, "keepalive");
    17. }
    18.  
    19. void UnkillApp::checkKeepClient()
    20. {
    21. int rc = s_send(socket, "areyoualive?");
    22.  
    23. const char* recm = s_recv(socket);
    24. if(recm == NULL)
    25. {
    26. QProcess::startDetached(m_cpath);
    27. }
    28. else if(strcasecmp(recm, "kill") == 0)
    29. {
    30. s_send(socket, "kill");
    31. p.setProgram(m_cpath);
    32. p.kill();
    33. }
    34. }
    35.  
    36. void UnkillApp::run()
    37. {
    38. while (b_run) {
    39. if(b_server)
    40. checkKeepServer();
    41. else
    42. checkKeepClient();
    43.  
    44. sleep(1);
    45. }
    46. }
    47.  
    48. void UnkillApp::runInBackground()
    49. {
    50. QtConcurrent::run(this, &UnkillApp::run);
    51. }
    52.  
    53. void UnkillApp::closethread()
    54. {
    55. b_run = false;
    56. }
    To copy to clipboard, switch view to plain text mode 

    and this is the main function

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. UnkillApp s("keepusA", true);
    6. s.runInBackground();
    7.  
    8. // MainForm w;
    9. MainWindow w;
    10. w.show();
    11.  
    12. QObject::connect(&w, SIGNAL(closed()), &s, SLOT(closethread()));
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    but after closing mainwindow in debug mode i can see b_run = false; called but application dont exit and i must kill it why? (and i dont run any other process yet)
    Last edited by danics; 21st November 2014 at 15:29.

  2. #2
    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: qtconcurrent dont close correctly

    You are accessing b_run from two different threads without any syncrhonisation.

    Not saying this is the reason for the observed behavior but wrong nevertheless.

    Cheers,
    _

Similar Threads

  1. midi child does not close when I call close()
    By qlands in forum Qt Programming
    Replies: 7
    Last Post: 29th July 2011, 22:25
  2. QT application not close after close the mainwindow
    By artome in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 22:23
  3. Replies: 2
    Last Post: 6th May 2011, 08:02
  4. How to correctly close a database connection
    By anoraxis in forum Qt Programming
    Replies: 3
    Last Post: 8th April 2011, 16:28
  5. Replies: 2
    Last Post: 17th December 2010, 19:01

Tags for this Thread

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.