This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I have a program where one thread feeds in data and another reads this data which does some calculations. The data is passed in the form of float[][]
buffers, allocated in the calculation thread. The feeder thread calls functions in the calculation object to assign an empty buffer for filling, and then to mark the buffer as ready for processing by storing the pointer in a std::vector
. The calculation thread checks this vector, and if a full buffer is ready, starts to work on the buffer.
However, on occasion, the calculation thread takes an invalid pointer from the vector, which understandably ends quite badly! On looking into it, the vector's data occasionally seems to be corrupted. This seems to occur directly after a newly filled buffer is added to the vector, which leads me to believe that, for some reason, the vector is part-way through being reallocated.
Below is the code for the functions to return a filled buffer (tryAssignFreeWriteBuffer - the code to assign an empty buffer has been removed), and the function to pull a filled buffer for calculating (tryGetFreeReadBuffer). I had thought it was due to threading issues, but even the output in tryAssignFreeWriteBuffer() can be corrupted, despite it all being in the same function - an example output can be found at this link. I really can't see why std::vector::push_back()
would return with the vector in an invalid state.
/*
* Marks the current write buffer as being ready for reading, and
* attempts to pull a new, free, write buffer.
* Returns false if no new write buffer is yet available.
*/
bool Correlator::tryAssignFreeWriteBuffer()
{
// First, check to see if there is a full write buffer to mark as readable
if(writeBuffer != NULL)
{
while(readBufferLock)
{
// Wait for read buffers to be unlocked
std::cout << "RBL tryAssignFWB\n";
}
readBufferLock = true;
std::cout << readBuffers.capacity() << " CapPre\n";
readBuffers.push_back(writeBuffer);
std::cout << readBuffers.capacity() << " CapPost\n";
readBufferLock = false;
std::cout << "List of RBs\n";
for(std::vector<float**>::iterator i = readBuffers.begin(); i != readBuffers.end(); i )
{
std::cout << *i << "\n";
}
}
// Snip //
}
/*
* Attempts to get a filled buffer to read from
* Also assigns the prevReadBuffer pointer
*/
bool Correlator::tryGetFreeReadBuffer()
{
// Assign previous read buffer pointer
if(readBuffer != NULL)
{
prevReadBuffer = readBuffer;
readBuffer = NULL;
}
while(readBufferLock)
{
// Wait for read buffers to be unlocked
std::cout << "RBL tryGetFreeRB\n";
}
readBufferLock = true;
// Retrieve any unread data
if(!readBuffers.empty())
{
// Get the first unread buffer
readBuffer = readBuffers.front();
/* std::cout << "List of RBs\n";
for(std::vector<float**>::iterator i = readBuffers.begin(); i != readBuffers.end(); i )
{
std::cout << *i << "\n";
}
*/
std::cout << "ReadBuffer " << readBuffer << "\n";
// Remove the newly loaded read buffer from the list of unread buffers
readBuffers.erase(readBuffers.begin());
readBufferLock = false;
return true;
}
// If there is no unread data left, check to see if
// the input system is done sending in data
if (allDataRead && readBuffers.empty())
{
writeToFile(isNewFile);
std::cout << "All data read \n";
stopCorrelating = true;
readBufferLock = false;
return false;
}
// Otherwise, report back that we couldn't get any more data
// The function should then be called again at a later time to
// see if any data has arrived yet
else
{
readBuffer = NULL;
readBufferLock = false;
return false;
}
}
Subreddit
Post Details
- Posted
- 10 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...