mercredi 24 juin 2020

Is it possible to create a thread that calls a function from a different class?

I am working on a C++11 multithreated GUI program.

So I have the DevTools and MainWindow classes. I want to call a function that belongs to DevTools from a MainWindow clicked() event function. (All object definitions are created on the classes' definitions).

Function on MainWindow calls Obtain_XY_Thread:

void MainWindow::on_obtain_xy_clicked()
{
    DevTools.Obtain_XY_Thread();
}

So DevTools::Obtain_XY_Thread() is called and it creates a thread of Obtain_XY but what I really want is directly call Obtain_XY:

void DevTools::Obtain_XY_Thread()
{
    thread obtain_xy(&DevTools::Obtain_XY, *this);
    obtain_xy.detach();
}

void DevTools::Obtain_XY()
{
    for(int i=0;i<500;i++){
        //does something
    }
}

This works, but I don't think it's the correct approach. Is there any way to call Obtain_XY without having to create another function? How could I create the thread on MainWindow::on_obtain_xy_clicked() directly?

Aucun commentaire:

Enregistrer un commentaire