c语言如何实现测试Windows 系统创建进程和线程的能力

如题所述

下面为C语言调用WIN API实现创建线程:
1,导入<windows.h>头文件
2,声明实现方法DWORD WINAPI ThreadProc1( LPVOID lpParam ) {}
3,在main()方法中调用 CreateThread(NULL,0 ,ThreadProc1,NULL,0,NULL);
要注意的是主线程不能结束,如果主线程结束,则它的子线程也会被杀死。
#include <windows.h>
#include <stdio.h>
#include<time.h>
DWORD WINAPI ThreadProc1( LPVOID lpParam )
{
int i=0;
time_t timer;
while(1)
{
timer=time(NULL);
printf("The current time is: %s\n",asctime(localtime(&timer)));
sleep(1);
}
}
void main()
{
int i=0;
//让主线程进入循环,主线程若退出,子线程1,2会被系统“杀死”
//创建线程1
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier
for(;;)
{
;
}
}追问

怎么会出错

温馨提示:答案为网友推荐,仅供参考
相似回答