Friday, 15 March 2013

c++ - How to stop a thread containing a while(1) -



c++ - How to stop a thread containing a while(1) -

i create in class mainwindow thread launch server, when close mainwindow, need stop server, can't because server running in while(true) loop. possibility saw kill thread containing server.

but it's still not possible because utilize #include <thread> , c++11.

my solution pass boolean server, , execute loop, while boolean true. can't out of loop when boolean become false, because server wait receive frame doesn't come before finish loop.

here's code :

mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); //some initialisation ... // constructs new thread , runs it. 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 frame } }

so, there way kill thread in c++11, or see way stop server ?

thanks.

if it's udp server, , want unblock read 'stop' boolean true, set boolean , send udp message on local stack, unblocking it.

c++ multithreading qt c++11 network-programming

No comments:

Post a Comment