CreateThread将std :: string作为参数传递

时间:2016-11-03 03:04:28

标签: c++ multithreading c++11 clr

我需要在托管C ++代码(CLR)中创建一个线程来调用一个非托管C ++类成员函数,并将std::string作为参数传递。正在调用该线程,但收到的std::string作为空字符串被接收:

托管代码:

std::string param;
CreateThread(0, NULL, (LPTHREAD_START_ROUTINE) &MyThread.Start, &MyThread, (DWORD) &param, NULL);

非托管代码:

class MyThread
{
    public:
        MyThread();
        static void Start(std::string &param);
};

void MyThread::Start(std::string &param)
{
    std::cout << param << std::endl; <<=== param is empty here
}

1 个答案:

答案 0 :(得分:1)

特别是在您的情况下,您将&MyThread作为线程函数参数传递,并将param作为CreateThread函数的dwCreationFlags参数传递,该函数指定线程创作选择。

此外,您需要确保在线程的生命周期内保持param

希望有所帮助。