Results 1 to 19 of 19

Thread: Using QUdpSocket within a QThread

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using QUdpSocket within a QThread

    Hopefully someone can help with this problem. I have created a QThread to work in conjunction with a GUI app. When runing the executable everything works great. However, after shutting down the application and trying to restart it within 5 seconds or so I get the following error:

    GThread-ERROR **: GThread system may only be initialized once.
    aborting...
    Segmentation fault

    I thought from the way may code was written that everything (includeing the thread) are terminated, but it appears that the thread is still running. How do I get it to stop completely. Using RedHat EL 4


    Thanks for the help.

    Here is my code:
    Qt Code:
    1. // Main -----------------------------------------------------------------
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. // creates class that contains packets and methods
    6. CHostIFTools * hostifTools = new CHostIFTools();
    7.  
    8. // create threads to handle UDP recieves and tranmits
    9. CHostIF hostifReceive( hostifTools );
    10.  
    11. // start independant threads
    12. hostifReceive.start();
    13.  
    14. // create application class
    15. QApplication app(argc,argv);
    16. CHostIF * hostif = new CHostIF( 0, hostifTools );
    17.  
    18. // display main widget
    19. hostif->show();
    20.  
    21. // run application
    22. app.exec();
    23.  
    24. // stop independant threads
    25. hostifReceive.quit();
    26.  
    27. // wait for thread to finish
    28. hostifReceive.wait();
    29.  
    30. // delay to ensure that threads are really terminated
    31. printf("Waiting for thread to shutdown");
    32. fflush(stdout);
    33.  
    34. for (int sleepIdx=0; sleepIdx<5; sleepIdx++)
    35. {
    36. printf("..");
    37. fflush(stdout);
    38. sleep (1);
    39. }
    40. printf("done!!\n");
    41. fflush(stdout);
    42.  
    43. // need to remove the tools class
    44. delete hostifTools;
    45.  
    46. return 0;
    47. }
    48.  
    49.  
    50. // Class definition ----------------------------------------------------------------------
    51.  
    52. class CHostIFReceive : public QThread
    53. {
    54. public:
    55.  
    56. // CHostIFReceive : class initialization method
    57. CHostIFReceive( CHostIFTools * tools=0 );
    58.  
    59. // ~CHostIFReceive : class destrcutor method
    60. ~CHostIFReceive();
    61.  
    62. // run : local implementation of the QThread run method
    63. void run();
    64.  
    65. // stop : used to set the stop variable
    66. void quit();
    67.  
    68. protected:
    69.  
    70. // defines the tools class that is to be used
    71. CHostIFTools * hostifTools;
    72.  
    73. // provides clean approch to terminting the thread
    74. volatile bool stopThreads;
    75. };
    76.  
    77. // Class code ----------------------------------------------------------------------------
    78.  
    79. CHostIFReceive::CHostIFReceive(CHostIFTools * tools)
    80. : QThread()
    81. {
    82. // instantiate configuration tools class
    83. hostifTools = tools;
    84.  
    85. // indicate thread is in a running state
    86. stopThreads = false;
    87. }
    88.  
    89. void CHostIFReceive::run()
    90. {
    91. // as long as thread has not been terminated
    92. while ( ! stopThreads )
    93. {
    94. // update the system value
    95. hostifTools->updateSystemInformation1();
    96.  
    97. // wait for a little while
    98. sleep( 1 );
    99.  
    100. }
    101. }
    102.  
    103. void CHostIFReceive::quit()
    104. {
    105. stopThreads = true;
    106. QThread::quit();
    107. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 29th October 2007 at 22:32. Reason: missing [code] tags

Similar Threads

  1. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23
  2. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51
  3. Qthread Issue?
    By vishal.chauhan in forum Newbie
    Replies: 3
    Last Post: 29th March 2007, 08:50
  4. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51

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
  •  
Qt is a trademark of The Qt Company.