So here is the code extract as it was before the mutex.
// One frame of data.
struct Frame
{
double x;
double y;
double z;
}
class FileLoader
{
// The data structure. A QList of Frame structs.
QList<Frame> data;
}
// This is the loader thread.
void FileLoader::run()
{
Frame f;
int frameCount = 0;
in >> frameCount;
for (int i = 0; i < frameCount; i++)
{
in >> f.x;
in >> f.y;
in >> f.z;
// Here is where a new frame is appended to the QList.
data << f;
if (i % 1000 == 0)
emit fileLoadingProgress(100*i/frameCount);
}
}
// The GUI class.
class GUI
{
FileLoader loader; // It has the FileLoader.
Frame* currentFrame; // And a pointer to the frame currently displayed on the screen.
double t; // This is a time parameter that is mapped to the data index.
double tscale; // This influences how fast the time grows (so the speed of the animation).
}
{
loader.start(); // It starts the FileLoader thread in the constructor.
t = 0.0;
tscale = 1.0;
}
// The animate function is periodically called by a timer.
// It changes the currentFrame pointer by mapping the time t to a data index.
void GUI::animate()
{
if (t + tscale < loader.data.size()-1)
{
t = t + tscale; // t is a double so that it can grow with arbitrary speed.
currentFrame = &(loader.data[(int)t]); // <-- currentFrame pointer assignment.
}
draw();
}
// One frame of data.
struct Frame
{
double x;
double y;
double z;
}
class FileLoader
{
// The data structure. A QList of Frame structs.
QList<Frame> data;
}
// This is the loader thread.
void FileLoader::run()
{
Frame f;
QFile file("data.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
int frameCount = 0;
in >> frameCount;
for (int i = 0; i < frameCount; i++)
{
in >> f.x;
in >> f.y;
in >> f.z;
// Here is where a new frame is appended to the QList.
data << f;
if (i % 1000 == 0)
emit fileLoadingProgress(100*i/frameCount);
}
}
// The GUI class.
class GUI
{
FileLoader loader; // It has the FileLoader.
Frame* currentFrame; // And a pointer to the frame currently displayed on the screen.
double t; // This is a time parameter that is mapped to the data index.
double tscale; // This influences how fast the time grows (so the speed of the animation).
}
GUI::GUI(QWidget *parent)
{
loader.start(); // It starts the FileLoader thread in the constructor.
t = 0.0;
tscale = 1.0;
}
// The animate function is periodically called by a timer.
// It changes the currentFrame pointer by mapping the time t to a data index.
void GUI::animate()
{
if (t + tscale < loader.data.size()-1)
{
t = t + tscale; // t is a double so that it can grow with arbitrary speed.
currentFrame = &(loader.data[(int)t]); // <-- currentFrame pointer assignment.
}
draw();
}
To copy to clipboard, switch view to plain text mode
Btw, I did not say that the mutex solved the problem. I just said I put it in and it didn't slow anything down. I need to observe the software for a while to see if the broken frames still occur.
Bookmarks