Results 1 to 12 of 12

Thread: multithread thread don't move

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Smile multithread thread don't move

    hello, i write a program that create 3 threads, and use QWaitCondition and QMutex let's them sync, but when the program run awhile, it stop in enqQue.wait(&mutex), and the program not die, it does not process data, please help me what wrong. thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: multithread thread don't move

    if you can post your code
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: multithread thread don't move

    this is my htree run functin code, this problem is the program run awhile, but after stoped in the second run() function's deqWait.wait(&mutQue), and now the dataBuff size is 8192(most big), the dataQue size is 0, please help what wrong. thanks

    Qt Code:
    1. void UrineThread::run()
    2. {
    3. int readlen, retval, i;
    4. char str[TEMPDATABUFSIZE];
    5. fd_set rfds;
    6. struct timeval tv ;
    7.  
    8. tv.tv_sec = 0;
    9. tv.tv_usec = 100000;
    10.  
    11. printf("thread....\n");
    12. while (!stopThread)
    13. {
    14. FD_ZERO(&rfds); // 清空串口接收端口集
    15. FD_SET(fd,&rfds); // 设置串口接收端口集
    16.  
    17. while(FD_ISSET(fd, &rfds)) // 检测串口是否有读写动作
    18. {
    19. FD_ZERO(&rfds); // 清空串口接收端口集 每次循环都要清空,否则不会检测到有变化
    20. FD_SET(fd,&rfds); // 设置串口接收端口集
    21. retval = select(fd+1,&rfds,NULL,NULL,&tv);
    22.  
    23. if(retval == -1)
    24. {
    25. printf("an error accured.\n");
    26. break;
    27. }
    28. else if(retval) //retval > 0
    29. {
    30. readlen = ::read(fd, str, TEMPDATABUFSIZE); //读取数据
    31.  
    32. printf("readlen value: %d\n", readlen);
    33. printf("data Buff size : %d\n", dataBuff.size());
    34.  
    35. for (i=0; i<readlen; ++i)
    36. {
    37. mutex.lock();
    38. while (dataBuff.size() >= DATABUFFSIZE)
    39. buffFull.wait(&mutex); //ç*‰å¾…
    40. dataBuff.enqueue(str);
    41. buffEmpty.wakeAll();
    42. enqWait.wakeAll(); //唤醒所有线程
    43. mutex.unlock();
    44. }
    45. /* str[readlen] = '\0'; //ç»™è¯»å–çš„æ•°æ®åŠ ç»“å°¾ç¬¦
    46.   dataBuff.append(str); */ //å°†æ•°æ®åŠ å…¥åˆ°ç¼“å†²åŒºä¸*
    47.  
    48. FD_ZERO(&rfds);
    49. FD_SET(fd,&rfds);
    50.  
    51. retval = select(fd+1,&rfds,NULL,NULL,&tv); //判æ–*是否还有数据
    52.  
    53. if(!retval) //如果没有数据则退出第二层循环
    54. {
    55. break;
    56. }
    57. }
    58. }
    59. msleep(60); //æ— æ•°æ®æ—¶ï¼Œç¡çœ 100毫秒, 新增的
    60. }
    61. }
    62.  
    63. void UrineShowThread::run()
    64. {
    65. bool result;
    66. PacketType currPack;
    67.  
    68. while (!stopThread)
    69. {
    70. mutQue.lock();
    71. while (packQue.size() < 1)
    72. deqWait.wait(&mutQue);
    73. printf("data Buff size: %d, data Que size: %d\n", dataBuff.size(), packQue.size());
    74. currPack = packQue.dequeue();
    75. enqWait.wakeAll();
    76. buffEmpty.wakeAll();
    77. mutQue.unlock();
    78.  
    79. result = sHostPackHandler[gHostPackInfo[currPack.ID].type]( currPack );
    80.  
    81. if (FALSE == result)
    82. {
    83. printf("pack handler fault.\n");
    84. // the command has not been processed, add code here to process it
    85. }
    86. }
    87. }
    88.  
    89. void UrineProcThread::run()
    90. {
    91. printf("data process.\n"); //在æ*¤å¤„åŠ æ•°æ®å¤„ç†ä»£ç 
    92.  
    93. gHostPackMan.MakePack(); //解包,并å*˜äºŽåŒ…队列ä¸*
    94. printf("out the make pack recursive\n");
    95. }
    96.  
    97. void PackMan::MakePack( void )
    98. {
    99. UCHAR currChar;
    100. BOOL result;
    101. // repeat untill no data in receive buffer
    102. while (!stopThread) //只有当所有数据都处理后才退出
    103. {
    104. mutex.lock();
    105. // if (dataBuff.size() < 1)
    106. while (dataBuff.size() < 1)
    107. buffEmpty.wait(&mutex);
    108. currChar = dataBuff.dequeue(); //从缓冲区ä¸*获得一个数据
    109. buffFull.wakeAll();
    110. mutex.unlock();
    111.  
    112. // packet ID has been received
    113. if (mPackIdGot)
    114. {
    115. // current byte is a valid packet data
    116. if ( 0x80 <= currChar )
    117. {
    118. // be careful: data stored begin from the second byte
    119. mCurrPack.buffer[mCurrPackLen] = currChar;
    120. ++mCurrPackLen;
    121. --mRestByte;
    122.  
    123. // whole packet has been received,
    124. if ( 0 >= mRestByte )
    125. {
    126. result = UnpackWithCheckSum( &mCurrPack.buffer[0], mCurrPackLen );
    127.  
    128. if (result) //解包成功
    129. {
    130. mutQue.lock();
    131. while (packQue.size() >= PACKQUEUESIZE)
    132. enqWait.wait(&mutQue);
    133. packQue.enqueue(mCurrPack); //把解的包放到队列ä¸*去
    134. deqWait.wakeAll();
    135. mutQue.unlock();
    136. }
    137. else
    138. {
    139. printf("check sum fault.\n");
    140. // mErrorPack ++;
    141. //
    142. // if( NULL != gPtrView )
    143. // {
    144. // ::PostMessage( gPtrView->m_hWnd, WM_COMM_ERROR, mErrorPack, 0 );
    145. // }
    146. }
    147. mPackIdGot = 0;
    148. }
    149. }
    150. // current byte is not a valid packet data, maybe is a packet ID,
    151. // unget it for further analysis
    152. else
    153. {
    154. // there must be a error, because current packet is not integral
    155. //mPacksReceived.Put(Pack_ErrPack);
    156. // mErrorPack ++;
    157. //
    158. // if( NULL != gPtrView )
    159. // {
    160. // ::PostMessage( gPtrView->m_hWnd, WM_COMM_ERROR, mErrorPack, 0 );
    161. // }
    162. printf("the data is fault.\n");
    163. mPackIdGot = 0;
    164.  
    165. mutex.lock();
    166. while (dataBuff.size() >= DATABUFFSIZE)
    167. buffFull.wait(&mutex); //ç*‰å¾…
    168. dataBuff.prepend(currChar); //currChar 可能是一个包, 重新插入队列
    169. buffEmpty.wakeAll(); //唤醒所有线程
    170. mutex.unlock();
    171. // mUART.mRxCharQue.Unget( );
    172. }
    173. }
    174. // packet ID has not been received
    175. else
    176. {
    177. // check whether currChar is a valid packet ID
    178. if ( ( mMaxPackID > currChar ) && ( 0 < mPackInfo[currChar].len ) )
    179. {
    180. // ****** >>> ****** //
    181. mRestByte = mPackInfo[currChar].len - 1 ;
    182. mCurrPackLen = 1;
    183. mCurrPack.ID = currChar;
    184. mPackIdGot = 1;
    185. printf("rest byte len: %d\n", mRestByte);
    186.  
    187. // if this kind of packet only has an ID, a whole packet received
    188. if ( 0 == mRestByte )
    189. {
    190. mutQue.lock();
    191. while (packQue.size() >= PACKQUEUESIZE)
    192. enqWait.wait(&mutQue);
    193. packQue.enqueue(mCurrPack); //把解的包放到队列ä¸*去
    194. deqWait.wakeAll();
    195. mutQue.unlock();
    196.  
    197. mPackIdGot = 0;
    198. }
    199. }
    200. // currChar is not a valid packet ID
    201. else
    202. {
    203. printf("fault id: %d\n", currChar);
    204. /* mErrorPack ++;
    205.  
    206.   if( NULL != gPtrView )
    207.   {
    208.   ::PostMessage( gPtrView->m_hWnd, WM_COMM_ERROR, mErrorPack, 0 );
    209.   }
    210.  
    211.   // there must be a error, because current packet is not integral
    212.   mPacksReceived.Put(Pack_UnknownPack);
    213.   */
    214. }
    215. }
    216. } // while
    217.  
    218. return;
    219. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 27th March 2011 at 09:42. Reason: missing [code] tags

  4. #4
    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: multithread thread don't move

    I hate to spoil your fun using threads but why don't you just use QSocketNotifier instead of all that C code?
    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.


  5. #5
    Join Date
    Mar 2011
    Posts
    42
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: multithread thread don't move

    yea, when i use thread before, i use the QSocketNotifier to listen the serial file, but i don't know why, the serial data lost sometime, and the interface will be die, so i change to use thread, i have no way.

  6. #6
    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: multithread thread don't move

    Well, you must have been doing something wrong then. QSocketNotifier does more or less the same you're doing here only that it... works. There is no need for any threads here.
    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. Multithread in multicore CPU
    By ^NyAw^ in forum General Discussion
    Replies: 12
    Last Post: 26th December 2017, 14:19
  2. I don't quite understand this multithread example
    By HelloDan in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2009, 08:58
  3. "Cannot move to target thread"
    By Pepe in forum Qt Programming
    Replies: 6
    Last Post: 13th August 2007, 09:58
  4. about multithread
    By Pang in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2007, 18:06
  5. Help me about multithread!
    By vql in forum Newbie
    Replies: 19
    Last Post: 8th February 2007, 15:01

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.