I have a main thread which starts the worker thread. This worker thread has a while(true) loop with a break statement and condition inside the loop.
This worker thread listens to the Socket connection for infinite amount of time without any timeout as it was required by my application
When I click on abort in the main thread I should be able to stop the while loop running in the worker thread.
I tired to use a atom or isInturrupt feature in the QT but seems like it will need to check those flags inside the loop.
while (true)
{
j = 0;
memset(framebuf, 0, ImageByteSize);
while (j < iImageByteSizeWithDescriptor)
{
//my loop waits here for data for unlimited amount of time
i = recv(ClientSocket, framebuf + j, iImageByteSizeWithDescriptor - j, 0);
if (i == 0) throw std::runtime_error("Transmission error: no data received");
j += i;
}
memcpy(images + (recv_frames * ImageByteSize), framebuf, ImageByteSize);
std::cout << "Frame num " << recv_frames << "n";
recv_frames++;
if (recv_frames >= nrImages)
{
complete = true;
break;
}
}
}
No matter whatever break condition there will be a condition where no new data is arriving but still my loop will be waiting for the data.
Can I run this loop in another thread and destroy that thread when i want to abort?
Will that be thread safe and without errors.
Note: When I abort then program I dont care about the saved data a bit. I fine If i lose all the data .
12