Reputation: 2503
I need your advice on a program I'm coding right now. First of all, let me present you what it is :
I design an IHM with button and stuff like this :
Here's my main window
:
When I click on the QButton Supervision
, i'm going on this window :
The IHM is finished, after I code a server, that works, and treat the frame receive. Now what I want to do is lauch the server and the IHM at the same time, and when I click on supervision, show the information that the server receive in the QTableWidget.
For doing this, I think I need : THREAD
AND SHARED MEMORY SEGMENTATION
.
Thread
: To lauch the server and the IHM , and Memory Segmentation
: to give to my IHM, the data that the server receive.
I try to lauch the server in the constructor of my main window
, here's the code in my main window
:
//Constructor
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Start my window (initialisation of button code
...
//Thread of the server
std::thread t1(lancerServeur);
t1.join();
}
//Server function lauch by thread
void MainWindow::lancerServeur(){
Server s;
while(1){
s.readData();
}
}
PROBLEM
: The server start but not the IHM, I don't understand why, because it is supposed to be in a thread ...
Also do you think shared memory segmentation is a good idea ? If yes, do you have a good link for me ?
Thanks.
Upvotes: 0
Views: 109
Reputation:
std::thread::join() blocks the current thread until the t1 thread finishes its execution.
You basically lock yourself into the t1 thread.
What to do?
store t1 somewhere else, so you can access it later, finnish what you were doing with the ihm thread etc. And when you are all set, access back t1 and call join().
What I see:
int main()
{
std::cout << "starting first thread...\n";
std::thread t1(foo);
std::cout << "starting second thread...\n";
std::thread t2(bar);
std::cout << "waiting for threads to finish..." << std::endl;
helper1.join();
helper2.join();
std::cout << "done!\n";
}
Upvotes: 1
Reputation: 2969
As your server reading thread is an infinite loop, you will never exit of it. So, a call to join
will never return. Storing the thread object in a member variable ensures it will not be destroyed once you 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 my window (initialisation of button code
...
//Thread of the server
m_t1 = std::thread(lancerServeur);
}
If you just want a thread runs by itself on its own and don't care that it is joinable, you can also decided to detach
it.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Start my window (initialisation of button code
...
//Thread of the server
std::thread t1 = std::thread(lancerServeur);
t1.detach();
//..going out of scope will not destroy the thread
}
Upvotes: 2