I create in my class MainWindow
a thread that launch a server
, when I close this MainWindow
, I need to stop that server, but I can't because the server running in a while(true) loop
. So the only possibility I saw is to kill the thread containing the server.
But it's still not possible because I use #include <thread>
and the C++11
.
My solution was so to pass a boolean
to my server, and to execute the loop, while this boolean is true. But I can't get out of my loop when my boolean become false, because my server wait to receive a frame that doesn't come before finish the loop.
Here's my code :
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Some initialisation
...
// Constructs the new thread and runs it. Does not block execution.
m_t1 = std::thread(lancerServeur);
}
MainWindow::~MainWindow()
{
delete ui;
m_t1.join();
}
void MainWindow::lancerServeur(){
serveur s;
while(true){
s.receiveDataUDP();//Wait for a frame
}
}
So, is there a way to kill my thread in C++11, or do you see a way to stop my server ?
Thanks.