如何将参数传递给每个线程? [英] How do I transfer parameter to each thread?

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

问题描述

在我的VC ++代码中



CWinThread * Serial_Thread [3];



_tmain(int argc ,char * argv [])

{



for(int i = 0; i< 3; i ++)

{

Serial_Thread [i] = AfxBeginThread(SendSerial_Thread,& i);

}

}



静态UINT SendSerial_Thread(LPVOID pParam)

{

int x =(int)pParam;

}



但是在调试时,

最后一个x线程不等于主程序的i;

(如果i = 0,x = 4060628 ....)



我认为这很简单。

但是现在,我不知道为什么或者我的错在哪里....

请让我知道



提前感谢。



我的尝试:


$ b这个问题多浪费2美元...

In my VC++ code

CWinThread* Serial_Thread[3];

_tmain(int argc, char* argv[])
{

for(int i = 0 ; i < 3; i++)
{
Serial_Thread[i] = AfxBeginThread(SendSerial_Thread, &i);
}
}
and
static UINT SendSerial_Thread(LPVOID pParam)
{
int x = (int)pParam;
}

But when debugging,
the last x of thread is not equal of i of main program;
(if i=0, x=4060628....)

I thought it is simple.
But now, I don't know why or where is my fault....
Please let me know

Thank in advance.

What I have tried:

2 more hours wasted for this problem...

推荐答案

pParam 是一个指针 to int不是int。应该是

pParam is a pointer to int not an int. Should be
static UINT SendSerial_Thread(LPVOID pParam)
{
    int x = *reinterpret_cast<PINT>(pParam);
}


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

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