绩效评估组织行为学:CreateThread为什么不能运行?
来源:百度文库 编辑:神马品牌网 时间:2024/05/05 21:16:09
# include <stdio.h>
//# include <windows.h>
# include <process.h>
# include <afx.h>
int main()
{
void printNumber();
void printChar();
HANDLE numThread,charThread;
DWORD numID,charID;
numThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)printNumber,NULL,0,&numID);
charThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)printChar,NULL,0,&charID);
return 0;
}
void printNumber()
{
int i;
for(i = 0; i != 101; ++i)
{
printf("%d\n",i);
Sleep(1000);
}
}
void printChar()
{
int j;
for(j = 0; j != 101; ++j)
{
printf("%c\n",j % 26);
Sleep(1000);
}
}
上面的程序为什么不能运行,编译没有任何问题,一运行就崩溃。
哪位有使用CreateThread且可以运行的example也可以贴上来,谢谢各位!
//# include <windows.h>
# include <process.h>
# include <afx.h>
int main()
{
void printNumber();
void printChar();
HANDLE numThread,charThread;
DWORD numID,charID;
numThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)printNumber,NULL,0,&numID);
charThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)printChar,NULL,0,&charID);
return 0;
}
void printNumber()
{
int i;
for(i = 0; i != 101; ++i)
{
printf("%d\n",i);
Sleep(1000);
}
}
void printChar()
{
int j;
for(j = 0; j != 101; ++j)
{
printf("%c\n",j % 26);
Sleep(1000);
}
}
上面的程序为什么不能运行,编译没有任何问题,一运行就崩溃。
哪位有使用CreateThread且可以运行的example也可以贴上来,谢谢各位!
创建完线程,主线程就结束了,子线程当然不会执行了。
并且线程函数原型应该是(CreateThread)
DWORD WINAPI ThreadProc(
LPVOID lpParameter
);
用WaitForMultiObjects来等子线程结束