Note that there are some explanatory texts on larger screens.

plurals
  1. POStuck in a circular wait condition. Need help to get out
    text
    copied!<p>I am currently developing an multi-threaded application that does raw video data playback using Qt. There are three worker threads. One is Reader Thread, one is Processor Thread and the other is a Video Output Thread. The reader thread will read raw data into a buffer, then the processor will process the raw data and put displayable data into a frame FIFO. </p> <p>My raw data buffer is implemented as a Hash table which means every frame index will have its corresponding raw data. Once the processor finish processing the raw data one a certain frame it deletes the raw data from the raw data buffer so that the reader can read data into the buffer once it comes back to a frame it read before.</p> <p>I now have problem when implementing pause functionality. What I wanna do is when the video is pause, the processor and reader should quit and video output thread will just display one same frame in the fifo. I can now exit from processor thread properly. The reader seems to get into a circular wait condition situation.</p> <p>Here are some code for my reader thread.</p> <pre><code> void ReaderThread::run() { forever { //inputState_ is internal state of the reader if (inputState_ == stop) // return from run() if state is stop return; //dataBuffer is a pointer to raw data buffer object //This grab the mutex lock of the raw data buffer dataBuffer-&gt;lock() //frameIndex is counter for frame if (dataBuffer-&gt;contains(frameIndex_)) { //mutex_ is a member variable of type QMutex mutex_.lock(); //Wait for the data to be removed. dataBuffer-&gt;waitForWritable(); mutex_.unlock(); } dataBuffer-&gt;unlock(); // Read data to raw frame buffer uchar rawDataPointer = readRawData(); dataBuffer-&gt;lock(); //insert the frame number and corresponding pointer into the hash table dataBuffer-&gt;insert(frameIndex, rawDataPointer); //wake up processor. dataBuffer-&gt;wakeForReadable() dataBuffer-&gt;unlock(); mutex_.lock(); if (inputState_ == play) frameIndex++; mutex_.unlock(); } } void ReaderThread::stop() { mutex_.lock(); inputState_ = stop; //Wake up the reader if the reader is waiting dataBuffer-&gt;wakeAllWritable(); mutex_.unlock(); // Wait for run() to return. QThread::wait(); } </code></pre> <p>Processor thread is implemented similarly. I stop the processor at first and it succeeded. I call ReaderThread::stop() from main thread when I want to return from run(). But I got into a circular wait condition, where if the reader thread is waiting on the buffer to be writable, it never will unlock mutex_ of this reader thread. As a result, the stop will always get block and the program just hangs. I tried not to lock the mutex_ before the dataBuffer->waitForWritable(), however, I got into a situation where the reader thread will wait on the writable right after the dataBuffer->wakeAllWritable() is called. </p> <p>Can anyone suggest a way to resolve this issue? I have been trying hard to figure out how to get out of this circular wait condition situation but didn't quite work out as I am still new to threaded programming. </p>
 

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