简体   繁体   English

C ++ Win32线程

[英]C++ Win32 Threads

I have some problems with using _beginthreadex . 我在使用_beginthreadex遇到一些问题。 How can I send function that was made by me to a thread? 如何将我创建的函数发送到线程? I am totally new to threads, its so silly question, but I can't handle it 我对线程完全陌生,这是一个很愚蠢的问题,但我无法解决

//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);
}

If you read the _beginthreadex documentation, you would see that the function you are passing in does not match the signature that _beginthreadex is expecting: 如果阅读_beginthreadex文档,则会看到传递的函数与_beginthreadex期望的签名不匹配:

unsigned ( __stdcall *start_address )( void * )

To do what you are attempting, you need a function of that exact signature to wrap your real function. 要执行您要尝试的操作,您需要具有确切签名的功能来包装您的实际功能。

Try something more like this instead: 尝试类似这样的方法:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM