C语言如何创建线程(windows)系统中
下面为C语言调用WIN API实现创建线程:
1,导入
2,声明实现方法DWORD WINAPI ThreadProc1( LPVOID lpParam ) {}
3,在main()方法中调用CreateThread(NULL,0 ,ThreadProc1,NULL,0,NULL);
要注意的是主线程不能结束,如果主线程结束,则它的子线程也会被杀死。
#include
#include
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(;;)
{
; } }