新线程运行问题
时间:2011-12-16
来源:互联网
#include <iostream>
#include <windows.h>
// 线程函数
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
for(int i = 0; i != 20; ++i)
{
std::cout << " I am from a thread, count = " << i << std::endl;
}
return 0;
}
int main(int argc, char* argv[])
{
HANDLE hThread;
DWORD dwThreadId;
// 创建一个线程
hThread = ::CreateThread (
NULL, // 默认安全属性
NULL, // 默认堆栈大小
ThreadProc, // 线程入口地址(执行线程的函数)
NULL, // 传给函数的参数
0, // 指定线程立即运行
&dwThreadId); // 返回线程的ID号
std::cout << " Now another thread has been created. ID = " << dwThreadId << std::endl;
// 等待新线程运行结束
::WaitForSingleObject (hThread, INFINITE);
::CloseHandle (hThread);
return 0;
}
VS2008编译运行结果
Now another thread has been created. ID = I am from a thread, count = 23680
I am from a thread, count = 1
I am from a thread, count = 2
I am from a thread, count = 3
I am from a thread, count = 4
I am from a thread, count = 5
I am from a thread, count = 6
I am from a thread, count = 7
I am from a thread, count = 8
I am from a thread, count = 9
I am from a thread, count = 10
I am from a thread, count = 11
I am from a thread, count = 12
I am from a thread, count = 13
I am from a thread, count = 14
I am from a thread, count = 15
I am from a thread, count = 16
I am from a thread, count = 17
I am from a thread, count = 18
I am from a thread, count = 19
请按任意键继续. . .
以上代码为什么没输出"i = 0"?
#include <windows.h>
// 线程函数
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
for(int i = 0; i != 20; ++i)
{
std::cout << " I am from a thread, count = " << i << std::endl;
}
return 0;
}
int main(int argc, char* argv[])
{
HANDLE hThread;
DWORD dwThreadId;
// 创建一个线程
hThread = ::CreateThread (
NULL, // 默认安全属性
NULL, // 默认堆栈大小
ThreadProc, // 线程入口地址(执行线程的函数)
NULL, // 传给函数的参数
0, // 指定线程立即运行
&dwThreadId); // 返回线程的ID号
std::cout << " Now another thread has been created. ID = " << dwThreadId << std::endl;
// 等待新线程运行结束
::WaitForSingleObject (hThread, INFINITE);
::CloseHandle (hThread);
return 0;
}
VS2008编译运行结果
Now another thread has been created. ID = I am from a thread, count = 23680
I am from a thread, count = 1
I am from a thread, count = 2
I am from a thread, count = 3
I am from a thread, count = 4
I am from a thread, count = 5
I am from a thread, count = 6
I am from a thread, count = 7
I am from a thread, count = 8
I am from a thread, count = 9
I am from a thread, count = 10
I am from a thread, count = 11
I am from a thread, count = 12
I am from a thread, count = 13
I am from a thread, count = 14
I am from a thread, count = 15
I am from a thread, count = 16
I am from a thread, count = 17
I am from a thread, count = 18
I am from a thread, count = 19
请按任意键继续. . .
以上代码为什么没输出"i = 0"?
作者: never772 发布时间: 2011-12-16
新手求教!
作者: never772 发布时间: 2011-12-16
楼主需要++i和i++区别!
作者: jiuzhoulh 发布时间: 2011-12-16
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28