gpt4 book ai didi

C++ Win32 线程

转载 作者:行者123 更新时间:2023-11-27 22:37:08 25 4
gpt4 key购买 nike

我在使用 _beginthreadex 时遇到了一些问题。如何将我创建的函数发送到线程?我对线程完全陌生,这是个愚蠢的问题,但我无法处理

//Function that I want to send to a thread

vector<int>F1(vector<int> A, vector<int> B, vector<int> C, vector<int> D, vector<vector<int>> MA, vector<vector<int>> MD) {
vector<int> ResVect;
ResVect = plusVector(plusVector(A, plusVector(B, C)), VectMultMatr(D, MultMatr(MA, MD)));
return ResVect;
}

//Thread funcF1

int main(){
HANDLE funcF1, funcF2, funcF3;
//////F1//////
cout << "Task 1 starts" << endl;
vector<int>A = createVect(1, 4);
vector<int>B = createVect(1, 4);
vector<int>C = createVect(1, 4);
vector<int>D = createVect(1, 4);

vector<vector<int>>MA = createMatrix(1, 4);
vector<vector<int>>MD = createMatrix(1, 4);

//vector<int>E = F1(A, B, C, D, MA, MD);
funcF1 = (HANDLE)_beginthreadex(0, 0, &F1(A, B, C, D, MA, MD), 0, 0, 0);
}

最佳答案

如果您阅读 _beginthreadex文档,您会看到您传入的函数与 _beginthreadex 期望的签名不匹配:

unsigned ( __stdcall *start_address )( void * )

要执行您正在尝试的操作,您需要具有该确切签名的函数来包装您的实际函数。

尝试更像这样的东西:

vector<int> F1(vector<int> A, vector<int> B, vector<int> C, vector<int> D, vector<vector<int>> MA, vector<vector<int>> MD) {    
return plusVector(plusVector(A, plusVector(B, C)), VectMultMatr(D, MultMatr(MA, MD)));
}

struct myVecs {
vector<int> A;
vector<int> B;
vector<int> C;
vector<int> D;
vector<vector<int>> MA;
vector<vector<int>> MD;
};

unsigned __stdcall myThreadFunc(void *arg) {
myVecs *vecs = (myVecs*) arg;
vector<int> E = F1(vecs->A, vecs->B, vecs->C, vecs->D, vecs->MA, vecs->MD);
// use E as needed...
delete vecs;
return 0;
}

int main(){
HANDLE funcF1;

//////F1//////
cout << "Task 1 starts" << endl;

myVecs *vecs = new myVecs;
vecs->A = createVect(1, 4);
vecs->B = createVect(1, 4);
vecs->C = createVect(1, 4);
vecs->D = createVect(1, 4);
vecs->MA = createMatrix(1, 4);
vecs->MD = createMatrix(1, 4);

funcF1 = (HANDLE) _beginthreadex(0, 0, &myThreadFunc, vecs, 0, 0);
if (func1 == 0) {
// error handling...
delete vecs;
}

// do other things as needed...

// wait for thread to terminate before exiting the app...
return 0;
}

关于C++ Win32 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52826522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com