//здесь ошибка: C3867: "MainWindow::GlobalPlus": нестандартный синтаксис; // используйте "&", чтобы создать указатель на член HANDLE pThread = CreateThread(NULL,0,GlobalPlus,0,0,NULL); //У функцию же есть один неявный параметр? LPTHREAD_START_ROUTINE MainWindow::GlobalPlus() { QString str = QString::number(this->GlobalData); //EnterCriticalSection(sec); for(int i = 0; i < 5; i++) { GlobalData+=3; Sleep(1); GlobalData-=3; str +="\n"+QString::number(this->GlobalData); ui->labThread1->setText(str); ui->labThread1->update(); } return 0; } 1 answer
First, in C ++, the name of a function of a non-static member of a class is not in itself a legal expression. That is just GlobalPlus - this is nonsense. Obtaining a pointer to a non-static member of a class always requires that the qualified name of this member be indicated, and also requires the explicit use of the & operator
&MainWindow::GlobalPlus Secondly, the function is a non-static member of a class in principle cannot act as a stream function. Therefore, what you are trying to do is basically impossible to do in this form. The hidden parameter this , of course, exists, but it will not miraculously fulfill the role of the LPVOID lpParameter parameter of the LPVOID lpParameter function LPVOID lpParameter .
Thirdly, why does your thread function suddenly return a pointer LPTHREAD_START_ROUTINE ? The stream function in WinAPI is required to return a DWORD and nothing else.
Therefore, do not be nonsense; make your function a static member of your class with an explicitly specified LPVOID lpParameter parameter. And, of course, pass a pointer to the corresponding object in CreateThread . (What object you have there is "appropriate" to me from here is not visible.)
/* static */ DWORD MainWindow::GlobalPlus(LPVOID lpParameter) { MainWindow *this_window = static_cast<MainWindow *>(lpParameter); // И далее работаем с `this_window` ... A more elegant realization of the same can be
/* static */ DWORD MainWindow::GlobalPlusCallback(LPVOID lpParameter) { MainWindow *this_window = static_cast<MainWindow *>(lpParameter); return this_window->GlobalPlus(); } In this case, you specify the GlobalPlusCallback in CreateThread , and your original GlobalPlus remains unchanged (except, of course, of the return type).
PS If you make the function static, then everything that I said in “first” can be ignored in this case.