返回信息流源程序如下
#include <windows.h>
#include <stdio.h>
DWORD WINAPI mythread(LPVOID para)
{
printf("Hello from the thread!\n");
return 0;
}
int main()
{
DWORD tid;
HANDLE hThread;
hThread = CreateThread(NULL, 0, mythread, NULL,0, &tid);
printf("Hello from the main program!\n");
WaitForSingleObject(hThread, INFINITE);
return 0;
}
为什么运行结果会是这样:
C:\program.exe
Hello from the main program!
Hello from the thread!
C:\program.exe
Hello from the main program!
Hello from the main program!
Hello from the thread!
Hello from the main program! 有时出现一次 有时出现两次.....
这是一条镜像帖。来源:北邮人论坛 / soft-design / #14348同步于 2007/1/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖
请问一个多线程的问题....
lsm041022
2007/1/8镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
把CreateThread改成_beginthreadex
CreateThread没有为新线程建立tiddata结构,所以运行属于CRT函数的printf会产生Memory Leak。。。关于CreateThread和_beginthreadex的具体区别《Windows核心编程》里将的很清楚了。。。
[QUOTE]
#include <windows.h>
#include <process.h>
#include <stdio.h>
unsigned __stdcall mythread(LPVOID para)
{
printf("Hello from the thread!\n");
_endthreadex(0);
return 0;
}
int main()
{
unsigned tid;
HANDLE hThread;
hThread = (HANDLE)_beginthreadex(NULL, 0, &mythread, NULL,0, &tid);
printf("Hello from the main program!\n");
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return 0;
} [/QUOTE]
我也很无聊。。。爬。。。= =