C++ win32 线程创建线程函数未正确传递参数
C++ win32 thread createThread function not passing parameter correctly
所以,我一直在试图弄清楚 c++ 多线程是如何工作的,以及如何将其应用于我正在从事的项目。我正在尝试完成创建新线程并在该线程上运行函数。我尝试运行的函数称为SetupInfo
,并将Individual
作为单个参数。我已经看到了这方面的示例并尝试实现它们,但是经过多次尝试,我无法成功地将我需要的参数传递到我希望函数运行的线程中。这是我想到的:
在这里,我创建一个结构来存储指向我稍后需要的Individual
的指针。
struct ThreadData
{
Individual *m_pInd;
ThreadData(Individual pInd) : m_pInd(*pInd) {}
};
在这里,我创建了一个可以在我的程序中调用的函数,该函数创建运行函数的线程SetupThreadFunction
该函数将 void 指针作为参数。我正在尝试将变量data
传递到此函数中,然后将其转换回ThreadData
以便能够访问结构的项目。
void SetupThread(Individual input)
{
ThreadData *data = new ThreadData(input);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) SetupThreadFunction, data , 0, 0);
delete data;
}
在这里,我创建了一个传递给 CreateThread
函数的函数,该函数采用 void 指针并将其转换为理论上可以访问threadData->m_pInd
的ThreadData
。上述data
的相同指针将正确传递到SetupThreadFunction
中。但是,m_pInd
包含空数据,而不是指向预期信息的指针。为什么?
DWORD WINAPI SetupThreadFunction(LPVOID lpParameter)
{
ThreadData* threadData = (ThreadData*)lpParameter;
SetupInfo(threadData->m_pInd);
return 0;
}
有没有更正确的方法将参数传递到我正在创建的新 win32 线程中?
正确的模式是使用 new
分配对象,填写数据(如果没有通过参数完成 new
(,将指针传递给新创建的线程,并让线程在完成对象时delete
对象。您在知道线程已经开始之前就delete
了对象!
这不是多线程问题;而是指针问题。
这行对我来说没有意义:
ThreadData(Individual pInd) : m_pInd(*pInd) {}
m_pInd
是一个指针,但你用*pInd
初始化它,这意味着你想取消引用pInd
,但pInd
不是指针,更不用说指向指针的指针了。 我看不出这甚至如何编译。
假设你实际上的意思是&
而不是*
,如:
ThreadData(Individual ind) : m_pInd(&ind) {}
这里的问题是,您正在创建指向堆栈上Individual
副本的指针,并且该副本在从构造函数返回时消失,因此您有一个悬而未决的指针。
使用 std::thread。
void ThreadProc(Individual individual);
int main()
{
Individual individual;
std::thread thread(ThreadProc, individual);
thread.join();
return 0;
}
下面是一个简单的代码示例来演示已经讨论过的要点。
#include "stdafx.h" // includes <windows.h>, <string> and <iostream>
using std::string;
using std::cout;
class Individual
{
public:
string s;
};
struct ThreadData
{
Individual *m_pInd;
ThreadData(Individual* pInd) : m_pInd(pInd) {}
};
DWORD WINAPI SetupThreadFunction(LPVOID lpParameter)
{
cout << "Hi From Threadn";
ThreadData* threadData = static_cast<ThreadData*>(lpParameter);
//SetupInfo(threadData->m_pInd);
// do delete here, once its finished with.
delete threadData;
return 0;
}
HANDLE SetupThread(Individual* input)
{
ThreadData *data = new ThreadData(input);
return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) SetupThreadFunction, data , 0, 0);
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hin";
Individual* i = new Individual;
HANDLE h = SetupThread(i);
if(h)
{
WaitForSingleObject(h, INFINITE);
cout << "Donen";
} else
{
cout << "Couldnt create threadn";
}
getchar();
delete i;
return 0;
}
请记住,您还可以使用 _beginthread
作为更简单的界面来启动 Win32 上的线程。
- C++线程中,没有重载函数接受 X 参数
- C++ 中的线程不能使用参数
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- C++线程找不到函数作为参数(链接器)
- 如何将带有参数的方法传递给线程以执行?
- 为什么 beginthreadex 线程参数变量在父线程中没有更新
- 如何将参数传递给已经开始运行的线程
- 线程构造函数周围的可变参数模板包装器无法编译
- 线程函数中的c++参数更改
- 参数包推导不一致 int 和 int& 在可变参数模板化成员函数中创建运行成员函数的线程
- 线程函数参数的前向声明不起作用
- 带参数的线程调用库函数
- 错误:静态断言失败:std ::线程参数必须在转换为rvalues后不可行
- 从从可调用参数创建的线程对象参数移动构造 C++11 线程
- C++ - 线程参数无缘无故增加
- 线程参数真实性
- 将对虚拟类实现的引用作为线程参数传递
- 当线程应该不同时,它们似乎得到了相同的线程参数(PThreads)
- c++多线程参数问题
- Windows c++线程参数传递