Problem with using WINAPI threads in Qt in C++

牧云@^-^@ 提交于 2021-01-29 20:19:54

问题


I was assigned to create a program that handles three WINAPI threads and reflects the workflow of those in progress bars. I decided to use Qt Widgets for these purposes. I create those threads in suspended state, using CREATE_SUSPENDED creation flag, then i resume it with ResumeThread function upon clicking the button. When I click it, the program crashes with an unhandled exception win32. Why could this happen?

My "create" button click slot

void MainWindow::on_pb_create_clicked()
{
    hThread[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadFunc1, NULL, CREATE_SUSPENDED, &thID[0]);
    hThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadFunc2, NULL, CREATE_SUSPENDED, &thID[1]);
    hThread[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadFunc3, NULL, CREATE_SUSPENDED, &thID[2]);
    threadsCreated = true;
}

My "Start" button click slot

void MainWindow::on_pb_start_clicked()
{
    if(threadsCreated)
    {
       for(int i = 0; i = 2; i++)
       {
           ResumeThread(hThread[i]);
       }
    }
    else
    {
        QMessageBox msg;
        msg.setWindowTitle("Error");
        msg.setText("Threads are not created");
        msg.exec();
    }
}

My thread function

DWORD WINAPI MainWindow::ThreadFunc1(LPVOID lpParams)
{
    for(int i = 0; i < 100; i++)
    {
        ui->progressBar->setValue(i);
        sleep(500);
    }
    return 0;
}

Other 2 thread functions are the same, only using different progress bar.


回答1:


You are accessing the ui apparatus from a non-ui thread. This will result in a race condition. To do what you want safely you will need to post QEvents to to ui thread. Alternatively use QThread and the signals and slots can achieve the same thing.




回答2:


It seems you are passing a member function pointer as the function that should be executed. Remember that you can't call a member function without an object instance that "performs" the function call, except for static member functions.

See this thread for a workaround: How do you use CreateThread for functions which are class members?

Bottom line is you call a static member function, pass it the object that should perform some member function call (your MainWindow instance in this case), then the instance performs the call.

Edit:

See the Microsoft Documentation for CreateThread under "Return value":

Note that CreateThread may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process.

https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread



来源:https://stackoverflow.com/questions/61728962/problem-with-using-winapi-threads-in-qt-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!