0

I try to remember of how threads work, I see that with C++11 it simplify the creation and utilisation of it. I use the answer to this post Simple example of threading in C++ for just create a simple thread.

But there's difference between me and the answer of the post, I'm not in a main, so I create my thread in a constructor, and it's not the same parameters.

Here's my simple code and what I try to do:

I'm in a class mainWindow.cpp :

//Constructor
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(lancerServeur, NULL);
    ui->setupUi(this);
}
void MainWindow::lancerServeur(){
    std::cout << "Le serveur se lance";
}

The errors are :

expected ';' before 't1'

statement cannot resolve address of overloaded function thread t1(lancerServeur, NULL);

I think my parameters for thread t1(lancerServeur, NULL); are false.

Could you explain me how it works ?

Thanks.

Community
  • 1
  • 1
Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76

1 Answers1

4

You use std::cout, so I'm assuming that the is no using namespace std; or the like somewhere before the thread. Try std::thread.

Try a lambda std::thread t1([this](){this->lancerServeur();});

Don't forget to th1.join() before exiting the constructor, else std::terminate will be called in the thread destructor.

If the thread in th1 will run for a while, then make it a class member variable and then the initialisation will look like th1 = std::move(std::thread t1([this](){this->lancerServeur();})); In the class destructor, th1.join();

Niall
  • 30,036
  • 10
  • 99
  • 142
  • I've got this when I use std in front of thread : no matching function for call to 'std::thread::thread(, NULL)' – Evans Belloeil Jun 26 '14 at 07:27
  • The function 'MainWindow::lancerServeur` should be static or use a lambda `std::thread([this](){this->lancerServeur();});` – Niall Jun 26 '14 at 07:29
  • If I put it static I've got this :no type named 'type' in 'class std::result_of' typedef typename result_of<_Callable(_Args...)>::type result_type; – Evans Belloeil Jun 26 '14 at 07:31
  • Remove the NULL, the `lancerServeur()` function doesn't take any arguments. – Niall Jun 26 '14 at 07:33
  • It compile ! But now : Invalid parameter passed to C runtime function. Happen when I try to lauch it. I wasn't thinking this was so hard to create a thread ^^' – Evans Belloeil Jun 26 '14 at 07:35
  • You've created the thread on the stack in your constructor. It probably is done by the time the constructor is complete. You need to wait for it to complete `th1.join()` else `std::terminate` will be called in the `std::thread` destructor. – Niall Jun 26 '14 at 07:38
  • It works, you could edit your answer, and I will accept it, but I've got a problem, this thread is supposed to be a server, so it won't stop, because i'm gonna put a while(1) in it, so if I use t1.join , it won't laucnh the application and stay in lancerServeur no ? – Evans Belloeil Jun 26 '14 at 07:41