c++ - Launch a server and an IHM in different thread -
i need advice on programme i'm coding right now. first of all, allow me nowadays :
i design ihm button , stuff :
here's main window :
when click on qbutton supervision, i'm going on window :
the ihm finished, after code server, works, , treat frame receive. want lauch server , ihm @ same time, , when click on supervision, show info server receive in qtablewidget.
for doing this, think need : thread , shared memory segmentation.
thread : lauch server , ihm , , memory segmentation : give ihm, info server receive.
i seek lauch server in constructor of main window, here's code in main window :
//constructor mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); //start window (initialisation of button code ... //thread of server std::thread t1(lancerserveur); t1.join(); } //server function lauch thread void mainwindow::lancerserveur(){ server s; while(1){ s.readdata(); } } problem : server start not ihm, don't understand why, because supposed in thread ...
also think shared memory segmentation thought ? if yes, have link me ?
thanks.
as server reading thread infinite loop, never exit of it. so, phone call join never return. storing thread object in fellow member variable ensures not destroyed 1 time go out of scope.
//header file #include <thread> class mainwindow : public qmainwindow { //... private: std::thread m_t1; }; //source file mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); //start window (initialisation of button code ... //thread of server m_t1 = std::thread(lancerserveur); } if want thread runs on own , don't care joinable, can decided detach it.
mainwindow::mainwindow(qwidget *parent) : qmainwindow(parent), ui(new ui::mainwindow) { ui->setupui(this); //start window (initialisation of button code ... //thread of server std::thread t1 = std::thread(lancerserveur); t1.detach(); //..going out of scope not destroy thread } c++ multithreading qt network-programming
No comments:
Post a Comment