multiple arguments to CreateThread function

旧巷老猫 提交于 2019-12-17 19:24:46

问题


When I use the CreateThread API method, what do I need to do when I want to pass more than one parameter where LPVOID lpParameter is passed?


回答1:


You can create a structure that holds all relevant data and pass a pointer to an instance of that structure (filled with the appropriate parameters) to CreateThread()

In your thread creation function you will need to cast the LPVOID back to a pointer to your structure to use it.




回答2:


Put those arguments into a struct, allocated on the heap, and pass the address of the struct in the LPVOID parameter. Your thread function can then cast the LPVOID to a pointer to struct and read out the parameters.

It is essential that you put it on the heap because if you put it on the stack of the thread that calls CreateThread then it can be invalid by the time your thread procedure tries to access it.



来源:https://stackoverflow.com/questions/10600111/multiple-arguments-to-createthread-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!