c++ - CreateThread 传递 std::string 作为参数

标签 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
}

最佳答案

特别是在您的情况下,您将 &MyThread 作为线程函数参数传递并将 param 作为 CreateThreaddwCreationFlags 参数传递函数,它指定线程创建选项。

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

希望对您有所帮助。

关于c++ - CreateThread 传递 std::string 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40393189/

相关文章:

c++ - 理解这个函数原型(prototype)

c++ - 为什么我不能创建一个包含 vec3 对象的 union ?

c++ - 如何使用 TBB 在单线程中运行一个函数

c++ - 在多线程环境中使用 libcurl 会导致与 DNS 查找相关的性能非常慢

c++ - 在程序中捕获 C++0x 标志

c++ - Openmp C++ 索贝尔边缘检测

c++ - Dev-C++ Hello world 不显示

linux - 生产者-消费者模式不适合我们的应用程序?或者只是使用上的误解?

c++ - std::array 的堆分配

c++ - 非类右值总是有 cv 非限定类型