[threading] ensure threads are terminated before exiting

This commit is contained in:
Jan Grewe 2020-03-16 11:46:49 +01:00
parent 1381ceda53
commit ffe4052832
3 changed files with 16 additions and 3 deletions

View File

@ -93,6 +93,14 @@ PylonRecorder::PylonRecorder(QWidget *parent)
} }
PylonRecorder::~PylonRecorder(){ PylonRecorder::~PylonRecorder(){
if (grabber->isRunning()) {
grabber->requestStop();
grabber->wait(1000);
}
if (writer->isRunning()) {
writer->forceStop();
writer->wait(1000);
}
delete pylon; delete pylon;
delete buffer; delete buffer;
delete grabber; delete grabber;

View File

@ -9,12 +9,13 @@ void Writer::run() {
int64_t count = 0; int64_t count = 0;
if (valid) { if (valid) {
stop_request = false; stop_request = false;
stopNow = false;
Pylon::CVideoWriter videoWriter; Pylon::CVideoWriter videoWriter;
videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType, videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType,
videoSpecs.fps, videoSpecs.quality); videoSpecs.fps, videoSpecs.quality);
videoWriter.Open(videoSpecs.filename.c_str()); videoWriter.Open(videoSpecs.filename.c_str());
MyImage img; MyImage img;
while (!stop_request || buffer->bufferLoad() > 0) { while ((!stop_request || buffer->bufferLoad() > 0) && !stopNow) {
if (buffer->bufferLoad() > 0 ) { if (buffer->bufferLoad() > 0 ) {
bool valid = buffer->pop(img); bool valid = buffer->pop(img);
if (valid) { if (valid) {
@ -32,7 +33,7 @@ void Writer::run() {
msleep(10); msleep(10);
} }
} }
} }
videoWriter.Close(); videoWriter.Close();
} else { } else {
std::cerr << "Got no video specifications, not writing!" << std::endl; std::cerr << "Got no video specifications, not writing!" << std::endl;

View File

@ -33,13 +33,17 @@ signals:
private: private:
ImageBuffer *buffer; ImageBuffer *buffer;
VideoSpecs videoSpecs; VideoSpecs videoSpecs;
bool stop_request = false; bool stop_request = false, stopNow = false;
bool valid = false; bool valid = false;
public slots: public slots:
void requestStop() { void requestStop() {
stop_request=true; stop_request=true;
} }
void forceStop() {
stopNow = true;
}
}; };
#endif // WRITER_H #endif // WRITER_H