Results 1 to 12 of 12

Thread: multithread thread don't move

  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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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

    Default Re: multithread thread don't move

    thank you, maybe i have something wrong, i paste my code of use QSocketNotifier


    Added after 7 minutes:


    this i use QSocketNotifier to notifier the serial.
    Qt Code:
    1. void UrineCheck::checkDataWrite(int m_fd)
    2. {
    3. m_notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
    4. QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(readSerialData()));
    5. }
    6.  
    7. static unsigned int bufflen = 2048;
    8. void UrineCheck::readSerialData()
    9. {
    10. int readlen, notReadlen;
    11. readlen = 0; //初始化已读数据大小
    12. char str[bufflen];
    13. notReadlen = bufflen - readlen; //初始化未读的数据大小
    14. QString string;
    15. QByteArray byteArray, byteHex;
    16.  
    17. byteArray.clear();
    18. byteHex.clear();
    19. string.clear();
    20.  
    21. while ((readlen =::read(fd, &str[bufflen-notReadlen], notReadlen)) > 0)
    22. { //没有数据可读时才退出循环
    23. notReadlen -= readlen;
    24.  
    25. if (0 >= notReadlen)
    26. {
    27. readlen = 0; //初始化
    28. notReadlen = bufflen - readlen;
    29. totalSize += bufflen;
    30. byteArray.clear(); //清除先前的数据
    31. byteHex.clear(); //清空先前的数据
    32. byteHex = byteArray.append(str).toHex(); //先转换为å*—节数组,再转换为16进制数据
    33. if ((index+8)%8 == 0) //æ*¤å¤–设了四个1024å*—节大小的缓冲区
    34. {
    35. index = 0;
    36. totalStr.remove(0, bufflen); //从缓冲区ä¸*åˆ é™¤æ—§æ•°æ®
    37. }
    38. else
    39. {
    40. ++index;
    41. }
    42. totalStr.append(byteHex); //å°†æ–°æ•°æ®åŠ å…¥ç¼“å†²åŒºä¸*
    43. }
    44. else
    45. {
    46. str[bufflen-notReadlen] = '\0';
    47. }
    48. textEdit->setPlainText(totalStr.mid(0, bufflen));
    49. usleep(100000); //100毫秒
    50. }
    51.  
    52. totalSize += bufflen - notReadlen;
    53. byteArray.clear(); //清除先前的数据
    54. byteHex.clear(); //清空先前的数据
    55. byteHex = byteArray.append(str).toHex(); //先转换为å*—节数组,再转换为16进制数据
    56. if ((index+8)%8 == 0) //æ*¤å¤–设了四个1024å*—节大小的缓冲区
    57. {
    58. index = 0;
    59. totalStr.remove(0, bufflen); //从缓冲区ä¸*åˆ é™¤æ—§æ•°æ®
    60. }
    61. else
    62. {
    63. ++index;
    64. }
    65. totalStr.append(byteHex); //å°†æ–°æ•°æ®åŠ å…¥ç¼“å†²åŒºä¸*
    66. textEdit->setPlainText(totalStr.mid(0, bufflen));
    67.  
    68. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by lzpmail; 27th March 2011 at 10:11.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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

    What kind of device does m_fd represent? Something like /dev/ttySX?
    Last edited by wysota; 27th March 2011 at 14:14.
    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.


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

    Default Re: multithread thread don't move

    m_fd is type of int, is open the serial file and return an id to stored in m_fd.

    the m_fd is same with in read fun's fd.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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'm not asking about the datatype, I'm asking what kind of stream it represents. Is it more like a fifo or like a regular file. In general you should be able to use it either via QLocalSocket or QFile without any special treatment. Your code should be as simple as:
    Qt Code:
    1. QLocalSocket *device = new QLocalSocket;
    2. device->setSocketDescriptor(m_fd);
    3. connect(device, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    4. // ...
    5. void X::onReadyRead() {
    6. QByteArray text = device->readAll();
    7. textEdit->setPlainText(text);
    8. }
    To copy to clipboard, switch view to plain text mode 

    The problem with your code is that you are thinking in terms of blocking API whereas Qt prefers non-blocking.
    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.


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

    Default Re: multithread thread don't move

    i will remember what you say, to say with you very happy. thanks. because the serial port will receive more data a second, i want to know the interface is will be die.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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

    Quote Originally Posted by lzpmail View Post
    because the serial port will receive more data a second, i want to know the interface is will be die.
    I have no idea what you mean by that.
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.