对一个例子的疑惑

GoAssemblyNow 2010-09-20 02:51:01
///////////////////////////////////////////////////////////////
// ThreadDemo.cpp文件



#include <stdio.h>
#include <windows.h>

// 线程函数
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
int i = 0;
while(i < 20)
{
printf(" I am from a thread, count = %d \n", i++);
}
return 0;
}

int main(int argc, char* argv[])
{
HANDLE hThread;
DWORD dwThreadId;

// 创建一个线程
hThread = ::CreateThread (
NULL, // 默认安全属性
NULL, // 默认堆栈大小
ThreadProc, // 线程入口地址(执行线程的函数)
NULL, // 传给函数的参数
0, // 指定线程立即运行
&dwThreadId); // 返回线程的ID号
问题在这里:dwThreadId没有确切的值,为什么能做参数呢?
printf(" Now another thread has been created. ID = %d \n", dwThreadId);

// 等待新线程运行结束
::WaitForSingleObject (hThread, INFINITE);
::CloseHandle (hThread);
return 0;
}
...全文
81 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
小雨好赖床 2010-09-20
  • 打赏
  • 举报
回复
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);

对于第二个参数 MSDN上解释
dwStackSize
[in] Initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable. For more information

如果是0, 则是默认大小,是NULL
GoAssemblyNow 2010-09-20
  • 打赏
  • 举报
回复
汗颜,貌似函数那章没学好啊。
jiuyuexuanhui 2010-09-20
  • 打赏
  • 举报
回复
相当于参数作为返回值呗 多了一个返回值
liwei3290 2010-09-20
  • 打赏
  • 举报
回复
这个没什么吧 楼主好好理解下
傻X 2010-09-20
  • 打赏
  • 举报
回复
是线程编号,写错了,本来就是空的。就好像一个函数要返回多个返回值光一个RETURN满足不了,那就直接放在参数里返回了
daiyouchun 2010-09-20
  • 打赏
  • 举报
回复
不懂111111111
傻X 2010-09-20
  • 打赏
  • 举报
回复
把新建的进程编号存入dwThreadId之中啊,你就可以查看了啊

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧