如何在CreateThread中传递多个参数 [英] how to pass multiple parameters in CreateThread

查看:101
本文介绍了如何在CreateThread中传递多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用CreateThread API方法时,当我想要
传递LPVOID lpParameter的多个参数时,我需要做什么?


Daniel

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?

Daniel

推荐答案

嗨Daniel,
Hi Daniel,

当我使用CreateThread API方法时,当我想要传递LPVOID lpParameter

的多个参数时,我需要做什么?
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?



定义一个类或结构。创建该类的实例,并将该指针作为lpParameter传递给该实例。

确保不使用基于堆栈的实例。要么使用堆分配

变量

还是静态存储。如果您使用静态存储,请仔细考虑

同步

访问该数据的多个线程。如果你使用堆分配的数据

不要

忘记在接收数据的线程中释放数据。


-

SvenC

Define a class or struct. Create an instance of that class and pass a
pointer
to that instance as lpParameter.
Be sure to not use a stack based instance. Either use a heap allocated
variable
or static storage. If you use static storage think carefully about
synchronizing
access of multiple threads to that data. If you use heap allocated data
don''t
forget to free that data in the thread which received the data.

--
SvenC


我没有意识到我甚至无法控制变量是否为堆栈/>
或堆-allocated。是什么决定了它的分配方式?


Daniel


" SvenC" < Sv *** @ nospam.nospamwrote in message

news:18 *************************** ******* @ microsof t.com ...
I didn''t realize I even had any control over whether the variable is stack
or heap -allocated. What determines how it is allocated?

Daniel

"SvenC" <Sv***@nospam.nospamwrote in message
news:18**********************************@microsof t.com...

嗨丹尼尔,
Hi Daniel,

>当我使用CreateThread API方法时,当我想传递LPVOID lpParameter传递的多个参数时,我需要做什么?
>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?



定义一个类或结构。创建该类的实例,并将该指针作为lpParameter传递给该实例。

确保不使用基于堆栈的实例。要么使用堆分配

变量

还是静态存储。如果您使用静态存储,请仔细考虑

同步

访问该数据的多个线程。如果你使用堆分配的数据

不要

忘记在接收数据的线程中释放数据。


-

SvenC


Define a class or struct. Create an instance of that class and pass a
pointer
to that instance as lpParameter.
Be sure to not use a stack based instance. Either use a heap allocated
variable
or static storage. If you use static storage think carefully about
synchronizing
access of multiple threads to that data. If you use heap allocated data
don''t
forget to free that data in the thread which received the data.

--
SvenC



你好Daniel。
Hi Daniel.

我没有意识到我甚至无法控制变量是否为
stack或heap -allocated。是什么决定了它的分配方式?
I didn''t realize I even had any control over whether the variable is
stack or heap -allocated. What determines how it is allocated?



请开始阅读一本关于C ++的书,然后才开始考虑编写多线程应用程序的
!!!


如果你想避免考虑内存管理,请使用C#。


为了帮助您入门:


struct MyData

{

int id;

std:string name;

};


void foo()

{

MyData d; //这是在堆栈上分配

MyData * pd = new MyData; //这是在堆上分配的


d.id = 42;

d.name ="当这个范围留下时我会死的;


pd-> id = 43;

pd-> name ="我住在堆栈上,所以关心在某处调用delete;


//如果您要存储d的地址并在foo离开后使用它

//您将访问随机数据


//您可以根据需要在流程中传递pd,但请确保

//在多个线程出现时同步访问并认为

//关于你调用删除的正确位置以避免泄漏内存

}

-

SvenC

Please, start reading a book about C++ before starting to think
about writing multi threaded apps!!!

If you want to avoid thinking about memory managment go with C#.

To get you started:

struct MyData
{
int id;
std:string name;
};

void foo()
{
MyData d; // this is allocated on the stack
MyData* pd = new MyData; // this is allocated on the heap

d.id = 42;
d.name = "I will die when this scope is left";

pd->id = 43;
pd->name = "I live on the stack, so care about calling delete somewhere";

// if you would store the address of d and use it after foo is left
// you would access random data

// you can pass around pd in your process as you like but be sure
// to synchronize access when multiple threads are around and think
// about the correct place where you call delete to avoid leaking memory
}
--
SvenC


这篇关于如何在CreateThread中传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆