返回信息流在主函数里有一个启动函数,我理解是相当于嵌套在主函数中
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
下面是老师给得多线程函数(举例)
DWORD WINAPI ThreadFunc( LPVOID lpParam)
{
DWORD x=*(DWORD*)lpParam;
WORD wColors[2];
wColors[0]=FOREGROUND_RED| FOREGROUND_INTENSITY;
wColors[1]=FOREGROUND_BLUE|FOREGROUND_GREEN| FOREGROUND_INTENSITY;
bool direction=true;
while(1)
{
if (x>65)
direction=false;
else if(x<3)
direction=true;
if (direction)
x+=2;
else
x-=2;
textout(handle,x,2,wColors,2,"HELLO C++");
Sleep(100);
textout(handle,x,2,wColors,2," ");
Sleep(100);
}
return 0;
}
请问我如果想从主函数传递一个handle过去,应该如何修改上面两段代码,谢谢
这是一条镜像帖。来源:北邮人论坛 / cpp / #12375同步于 2008/9/11
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
关于工作者线程(多线程)的一个问题
dreamfancy
2008/9/11镜像同步10 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
【 在 rebirthatsix 的大作中提到: 】
: 用参数,或者duplicatehandle
请问在哪加参数?启动函数里没有地方写啊 写在哪
在主函数里有一个启动函数,我理解是相当于嵌套在主函数中
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
入口之后的参数不就是传递给线程函数的参数么,你现在传的是个dword,也可以传句柄啊,也可以传数据结构的指针啊。。。
真是不好意思,我是初学者。哪个是入口之后的参数,其实具体哪个参数是干什么的看后面注释也不明白,不过还是请具体指出是哪个参数,屏幕上没有dword啊
【 在 rebirthatsix 的大作中提到: 】
:
: 在主函数里有一个启动函数,我理解是相当于嵌套在主函数中
: hThread = CreateThread(
: ...................
第四个
&dwThrdParam, // argument to thread function
好好看下MSDN吧~~
【 在 dreamfancy 的大作中提到: 】
: 真是不好意思,我是初学者。哪个是入口之后的参数,其实具体哪个参数是干什么的看后面注释也不明白,不过还是请具体指出是哪个参数,屏幕上没有dword啊
HANDLE handle = xxx;
hThread = CreateThread(
NULL,
0,
ThreadFunc,
&handle,
0,
&dwThreadId);
线程函数里面第一句话:HANDLE handle = *(HANDLE*)lpParam;
好好看下MSDN吧~~
要强制类型转换
DWORD WINAPI b( LPVOID lpParam )
{
int *p=(int*)lpParam;
...
}
LPVOID 就是 void *
typedef void* LPVOID