Results 1 to 3 of 3

Thread: Mutlithreading hangs gui

  1. #1
    Join Date
    Jun 2009
    Location
    Abu Dhabi
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Mutlithreading hangs gui

    First of all , i need to tell I'm a very newb to qt.

    I want to create a multithreading program for RingBuffer. I done it, but while the thread running it hangs the gui.

    I call the two thread for read from and write on ringbuffer by the push button signal. While the thread process going on , i can't access the gui ..Is anything wrong in my program.

    Below i paste my program. I have the ring Buffer as a seperate class file. If u want i will give that too..


    //Header file
    Qt Code:
    1. #ifndef RING_QTHREAD_H
    2. #define RING_QTHREAD_H
    3.  
    4. #include <QWidget>
    5. #include <QtGui>
    6. #include <QThread>
    7. #include <QMutex>
    8.  
    9. #include "ringBuffer.hh"
    10.  
    11. #define BUFF_SIZE 10
    12.  
    13. namespace Ui {
    14. class Ring_Qthread;
    15. }
    16.  
    17. class Ring_Qthread : public QWidget {
    18. Q_OBJECT
    19. public:
    20. Ring_Qthread(QWidget *parent = 0);
    21. ~Ring_Qthread();
    22.  
    23.  
    24. protected:
    25. void changeEvent(QEvent *e);
    26. bool eventFilter(QObject *, QEvent *);
    27.  
    28. private slots:
    29. void slot_load_buffer();
    30. void slot_start_process();
    31. void slot_quite_me();
    32.  
    33. private:
    34. Ui::Ring_Qthread *ui;
    35.  
    36. void fn_create_conn(void);
    37.  
    38. };
    39.  
    40. class Writer : public QThread
    41. {
    42. public:
    43. Writer();
    44. void run();
    45. };
    46.  
    47. class Reader : public QThread
    48. {
    49. public:
    50. Reader();
    51. void run();
    52. };
    53.  
    54. /*
    55.  * Start the Structure for input and output of RingBuffer.
    56.  */
    57.  
    58. typedef struct struct_buff
    59. {
    60. int xval;
    61. int yval;
    62.  
    63. float xtime;
    64. float ytime;
    65. }struct_buff_t;
    66.  
    67. /*
    68.  * End of strucutre.
    69.  */
    70.  
    71.  
    72. #endif // RING_QTHREAD_H
    To copy to clipboard, switch view to plain text mode 


    //Sourcefile...

    Qt Code:
    1. #include "ring_qthread.h"
    2. #include "ui_ring_qthread.h"
    3.  
    4.  
    5.  
    6. //Global Variables
    7. struct_buff_t in_buff[BUFF_SIZE+1], out_buff[BUFF_SIZE+1];
    8. QMutex mut_check;
    9. QWaitCondition cond_check;
    10. int i_return = 0;
    11. long int i_tot_count = 0;
    12. long int i_out_count = 0;
    13. bool b_inp = false;
    14. bool b_out = false;
    15. //i_tot_count=i_out_count=0;
    16.  
    17.  
    18. /*
    19.  * Start of Predefined Functions
    20.  */
    21.  
    22. Ring_Qthread::Ring_Qthread(QWidget *parent) :
    23. QWidget(parent),
    24. ui(new Ui::Ring_Qthread)
    25. {
    26. ui->setupUi(this);
    27. fn_create_conn();
    28.  
    29. }
    30.  
    31.  
    32. Ring_Qthread::~Ring_Qthread()
    33. {
    34. delete ui;
    35. }
    36.  
    37. void Ring_Qthread::changeEvent(QEvent *e)
    38. {
    39. QWidget::changeEvent(e);
    40. switch (e->type())
    41. {
    42. qDebug() << "omg";
    43. case QEvent::LanguageChange:
    44. ui->retranslateUi(this);
    45. break;
    46. default:
    47. break;
    48. }
    49. }
    50.  
    51. /*
    52.  * End of Pre-defined Functions
    53.  */
    54.  
    55.  
    56. /*
    57.  * Event filter function for get call after mouse click on all buttons.
    58.  */
    59.  
    60.  
    61. bool Ring_Qthread::eventFilter(QObject *_obj, QEvent *_event)
    62. {
    63.  
    64. if(_event->type() == QEvent::MouseButtonPress)// && ((QMouseEvent*)_event)->button() == Qt::LeftButton)
    65. {
    66. qDebug() << "This is Event filter to help u\n";
    67. if(_obj->objectName() == "btn_quit")
    68. {
    69. qDebug() << "btn_quit";
    70. emit slot_quite_me();
    71. }
    72. else if (_obj->objectName() == "btn_process")
    73. {
    74. qDebug() << "btn_process";
    75. emit slot_start_process();
    76. }
    77. else if (_obj->objectName() == "btn_load")
    78. {
    79. qDebug() << "btn_load";
    80. emit slot_load_buffer();
    81. }
    82. return true;
    83.  
    84. }
    85. else
    86. return false;
    87. }
    88.  
    89.  
    90. /*
    91.  * Start of create connections Functions.
    92.  */
    93. void Ring_Qthread::fn_create_conn()
    94. {
    95.  
    96. //Use the below commented code if u r gonna use slot and signals
    97.  
    98. connect(ui->btn_load, SIGNAL(clicked()), this, SLOT(slot_load_buffer()));
    99. connect(ui->btn_process, SIGNAL(clicked()), this, SLOT(slot_start_process()));
    100. connect(ui->btn_quit, SIGNAL(clicked()), this, SLOT(slot_quite_me()));
    101.  
    102. /*
    103.   ui->btn_process->installEventFilter(this);
    104.   ui->btn_quit->installEventFilter(this);
    105.   ui->btn_load->installEventFilter(this);
    106.   */
    107. }
    108.  
    109. /*
    110.  * End of Create connections Functions
    111.  */
    112.  
    113. /*
    114.  * Start of Slot Functions.
    115.  */
    116.  
    117. void Ring_Qthread::slot_load_buffer()
    118. {
    119. for (int i=0; i<BUFF_SIZE; ++i)
    120. {
    121. in_buff[i].xval = rand() % 10;
    122. in_buff[i].yval = rand() % 10;
    123.  
    124. in_buff[i].xtime = rand() % 500;
    125. in_buff[i].ytime = rand() % 500;
    126. }
    127. in_buff[BUFF_SIZE].xval = -1;
    128. in_buff[BUFF_SIZE].yval = -1;
    129.  
    130. in_buff[BUFF_SIZE].xtime = -1;
    131. in_buff[BUFF_SIZE].ytime = -1;
    132.  
    133. QFile *in_file;
    134. in_file = new QFile("input_buffer.txt");
    135. if(!in_file->open(QIODevice::WriteOnly | QIODevice::Text))
    136. qDebug() << "Error in open the input Buffer file";
    137. QTextStream stream_in(in_file);
    138. for (int j=0;j<=BUFF_SIZE;j++)
    139. {
    140. stream_in << in_buff[j].xval << " " << in_buff[j].yval << " " << in_buff[j].xtime << " " << in_buff[j].ytime << "\n";
    141.  
    142. }
    143.  
    144. }
    145.  
    146. void Ring_Qthread::slot_start_process()
    147. {
    148.  
    149. Writer writer1;
    150. Reader reader1;
    151.  
    152. writer1.start();
    153. reader1.start();
    154.  
    155. writer1.wait();
    156. reader1.wait();
    157.  
    158. return (void)NULL;
    159.  
    160.  
    161.  
    162. }
    163.  
    164. void Ring_Qthread::slot_quite_me()
    165. {
    166. exit(0);
    167. }
    168.  
    169. ///////////////////////////////////////////////////////////////
    170. /*
    171.  * Start of Class Writer Functions.
    172.  */
    173.  
    174. Writer::Writer() : QThread()
    175. {
    176. //Code if u want.
    177. }
    178.  
    179. void Writer::run()
    180. {
    181. //i_return = 3;
    182. for (int j=0; j<=BUFF_SIZE; j++)
    183. {
    184. qDebug() << in_buff[j].xval << " " << in_buff[j].yval << " " << in_buff[j].xtime << " " << in_buff[j].ytime << "\n";
    185. }
    186.  
    187. forever
    188. {
    189. mut_check.lock();
    190. if (i_return !=4)
    191. {
    192. i_return = fn_process(1, &in_buff[i_tot_count].xval, &in_buff[i_tot_count].yval, &in_buff[i_tot_count].xtime, &in_buff[i_tot_count].ytime);
    193. if (i_return == 4)
    194. {
    195. cond_check.wait(&mut_check);
    196. }
    197. else if (i_return == 2)
    198. {
    199. //qDebug() << "Writer \n";
    200.  
    201. i_tot_count++;
    202. //cond_check.wait(&mut_check);
    203. }
    204. if(in_buff[i_tot_count-1].xval == -1)
    205. b_inp = true;
    206. }
    207. mut_check.unlock();
    208.  
    209. if (b_inp && b_out)
    210. return (void) NULL;
    211. }
    212.  
    213. }
    214.  
    215. /*
    216.  * End of Class Writer Functions.
    217.  */
    218.  
    219.  
    220.  
    221. ///////////////////////////////////////////////////////////////
    222. /*
    223.  * Start of Reader Class Functions
    224.  */
    225.  
    226. Reader::Reader() : QThread()
    227. {
    228. //Code if u want
    229. }
    230.  
    231. void Reader::run()
    232. {
    233. forever
    234. {
    235. mut_check.lock();
    236. if(i_return !=1)
    237. {
    238. i_return = fn_process(2, &out_buff[i_out_count].xval, &out_buff[i_out_count].yval, &out_buff[i_out_count].xtime, &out_buff[i_out_count].ytime);
    239.  
    240. if(i_return == 3)
    241. {
    242. //qDebug() << "reader\n";
    243.  
    244. i_out_count++;
    245. //cond_check.wakeOne();
    246. }
    247. else if (i_return == 1)
    248. {
    249. qDebug() << "go man go\n";
    250. cond_check.wakeOne();
    251. }
    252. if (out_buff[i_out_count-1].xval == -1)
    253. b_out = true;
    254. }
    255. mut_check.unlock();
    256.  
    257. if (b_inp && b_out)
    258. {
    259. qDebug() << "\nOutPut man.\n";
    260. for (int j=0; j<=BUFF_SIZE; j++)
    261. {
    262. qDebug() << out_buff[j].xval << " " << out_buff[j].yval << " " << out_buff[j].xtime << " " << out_buff[j].ytime << "\n";
    263. }
    264. return (void)NULL;
    265. }
    266. }
    267. }
    268.  
    269. /*
    270.  * End of Reader Class functions.
    271.  */
    272.  
    273. ///////////////////////////////////////////////////////////////
    To copy to clipboard, switch view to plain text mode 
    Last edited by thahir1986; 28th September 2010 at 08:10. Reason: spellling correction

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Mutlithreading hangs gui

    Try debugging your program. Set some breakpoints and see where it loops infinitly.

    What the #@* is this?
    Qt Code:
    1. return (void)NULL;
    To copy to clipboard, switch view to plain text mode 

    By the way: asking the same question on multiple forums is considered rude by me.

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

    Default Re: Mutlithreading hangs gui

    You are starting a thread and immediately calling wait() which blocks the calling thread (your gui thread) until the thread it is waiting for exits.
    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. Linux QT4 app hangs on exit
    By doggrant in forum Qt Programming
    Replies: 6
    Last Post: 28th August 2009, 16:01
  2. Application Hangs in QTcpConnection
    By navi1084 in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 05:21
  3. Qt 4.5 Embedded hangs
    By nickich in forum Qt Programming
    Replies: 2
    Last Post: 2nd April 2009, 11:15
  4. QProcess hangs
    By Skizmo in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2007, 10:27
  5. Using COM(ActiveX?) hangs up
    By gri in forum Qt Programming
    Replies: 4
    Last Post: 6th March 2007, 22:05

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