Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting And Reconstructing Singleton in C++
    primarykey
    data
    text
    <p>I have an application which runs on a controlling hardware connected with different sensors. On loading the application, it checks the individual sensors one by one to see whether there is proper communication with the sensor according to predefined protocol or not.</p> <p>Now, I have implemented the code for checking the individual sensor communication as a singleton thread and following is the run function, it used select system call and pipe for interprocess communication to signal the end of thread.</p> <pre><code>void SensorClass::run() { mFdWind=mPort-&gt;GetFileDescriptor(); fd_set readfs; int max_fd = (mFdWind &gt; gPipeFdWind[0] ? mFdWind : gPipeFdWind[0]) + 1; int res; mFrameCorrect=false; qDebug("BEFORE WHILE"); while(true) { qDebug("\n IN WHILE LOOP"); usleep(50); FD_ZERO(&amp;readfs); FD_SET(mFdWind,&amp;readfs); FD_SET(gPipeFdWind[0],&amp;readfs); res=select(max_fd,&amp;readfs,NULL,NULL,NULL); if(res &lt; 0) perror("Select Failed"); else if(res == 0) puts("TIMEOUT"); else { if(FD_ISSET(mFdWind,&amp;readfs)) { puts("*************** RECEIVED DATA ****************"); mFrameCorrect=false; FlushBuf(); //int n=mPort-&gt;ReadPort(mBuf,100); int n=mPort-&gt;ReadPort(mBuf,100); if(n&gt;0) { Count++; QString str((const char*)mBuf); //qDebug("\n %s",qPrintable(str)); //See if the Header of the frame is valid if(IsHeaderValid(str)) { if( (!IsCommaCountOk(str)) || (!IsChecksumOk(str,mBuf)) || (!CalculateCommaIndexes(str)) ) { qDebug("\n not ok"); mFrameCorrect=false; } //if frame is incorrect else { qDebug("\n OK"); mFrameCorrect=true; }//if frame is correct(checksum etc are ok) }//else if header is ok }//if n &gt; 0 }//if data received FD_ISSET if(FD_ISSET(gPipeFdWind[0],&amp;readfs)) break; }//end nested else res not &lt;= 0 }//infinite loop } </code></pre> <p>The above thread is run started from the main GUI thread. This runs fine. The problem is I have given an option to the user to retest the subsystem at will. For this I delete the singleton instance using </p> <pre><code>delete SensorClass::instance(); </code></pre> <p>and then restart the singleton using</p> <pre><code>SensorClass::instace()-&gt;start(); </code></pre> <p>The problem is this time the control comes out of while loop in run() function immedeately upon entering the while loop, my guess is the pipe read has again read from the write pipe which was written to the last time. I have tried to use the fflush() to clear out the I/O but no luck. My question is </p> <ol> <li><p>Am I thinking on the right track?</p></li> <li><p>If yes then how do we clear out the pipes?</p></li> <li><p>If not can anyone suggest why is the selective retest not working?</p></li> </ol> <p>Thanks in advance..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload