C++ 传递给新线程的结构具有错误的值

C++ 传递给新线程的结构具有错误的值,c++,multithreading,struct,C++,Multithreading,Struct,我需要将多个值传递给一个新线程(使用\u beginthreadex函数创建)。我为此创建了一个结构 struct CaptureThreadInput { std::mutex* eventMutex; std::condition_variable* eventCv; std::vector<DispatchEvent>* eventVector; int capId, width, height; }; 在启动的线程中,我只调试输出。captu

我需要将多个值传递给一个新线程(使用
\u beginthreadex
函数创建)。我为此创建了一个结构

struct CaptureThreadInput {
    std::mutex* eventMutex;
    std::condition_variable* eventCv;
    std::vector<DispatchEvent>* eventVector;
    int capId, width, height;
};
在启动的线程中,我只调试输出。
capturatRead::initThread
是一个公共静态函数

unsigned int __stdcall CaptureThread::initThread(void* in) {
    CaptureThreadInput* data = (CaptureThreadInput*)in;
    cout << "Address from thrd " << std::addressof(*data) << "\n";
    cout << "Mutex from thread " << std::addressof(*(data->eventMutex)) << "\n";
    cout << "CapId from thread " << data->capId << "\n";
    cout << "Width from thread " << data->width << "\n";
    return 0;
}
我还尝试注释出
eventMutex
eventCv
eventVector
,只在结构中留下整数,结果相同

你知道是什么原因造成的吗?我如何修改代码来修复它


使用Microsoft Visual Studio for Windows x64平台进行开发。

您正在本地创建
参数
,然后将引用传递到
\u beginthreadex
,该引用立即返回。此时,您的
startcapturatread
退出,导致
params
超出范围。防止
params
超出范围,或者通过值传递
params
本地创建
params
,然后将引用传递到
\u beginthreadex
,该引用立即返回。此时,您的
startcapturatread
退出,导致
params
超出范围。或者防止<代码> PARAMS <代码>不在范围内,或者通过值< > PARAMS> <代码> >值< /p>为什么不使用C++标准线程?你能为我做一个测试吗?您可以将创建的
struct
存储到成员变量中,然后使用该变量传递到
\u beginthreadex
,而不是本地创建
capturaReadInput
。您正在本地创建
capturaReadInput
,然后将引用传递到
\u beginthreadex
,该引用立即返回。此时,您的
startcapturatread
退出,导致
params
超出范围。无论是防止代码> PARAMS <代码>,还是通过C++,通过代码> > PARAMS <代码>,你实际上是幸运的,你得到了不同的值,而不是意外的相同的值,这将隐藏了问题。为什么不使用C++标准线程?你能为我做一个测试吗?您可以将创建的
struct
存储到成员变量中,然后使用该变量传递到
\u beginthreadex
,而不是本地创建
capturaReadInput
。您正在本地创建
capturaReadInput
,然后将引用传递到
\u beginthreadex
,该引用立即返回。此时,您的
startcapturatread
退出,导致
params
超出范围。要么防止
params
超出范围,要么按值传递
params
。实际上,幸运的是,您得到了不同的值,而不是意外地得到了相同的值,这会将问题隐藏起来。
unsigned int __stdcall CaptureThread::initThread(void* in) {
    CaptureThreadInput* data = (CaptureThreadInput*)in;
    cout << "Address from thrd " << std::addressof(*data) << "\n";
    cout << "Mutex from thread " << std::addressof(*(data->eventMutex)) << "\n";
    cout << "CapId from thread " << data->capId << "\n";
    cout << "Width from thread " << data->width << "\n";
    return 0;
}
Address from dispatch 0000001A83EFF568
Mutex from dispatcher 0000001A83EFFD10
CapId from dispatcher 0
Width from dispatcher 1280
Address from thrd 0000001A83EFF568
Mutex from thread CCCCCCCCCCCCCCCC
CapId from thread -858993460
Width from thread -858993460