我需要在托管 C++ 代码 (CLR) 中创建一个线程来调用非托管 C++ 类成员函数,并将 std::string
作为参数传递。正在调用线程,但接收到的 std::string
是作为空字符串接收的:
托管代码:
std::string param;
CreateThread(0, NULL, (LPTHREAD_START_ROUTINE) &MyThread.Start, &MyThread, (DWORD) ¶m, NULL);
非托管代码:
class MyThread
{
public:
MyThread();
static void Start(std::string ¶m);
};
void MyThread::Start(std::string ¶m)
{
std::cout << param << std::endl; <<=== param is empty here
}
最佳答案
特别是在您的情况下,您将 &MyThread
作为线程函数参数传递并将 param
作为 CreateThread
的 dwCreationFlags
参数传递函数,它指定线程创建选项。
此外,您需要确保在线程的生命周期内保留param
。
希望对您有所帮助。
关于c++ - CreateThread 传递 std::string 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40393189/