同一进程的多线程之间的切换时间粗侧

时间:2021-11-24 21:21:12
测试环境:X86架构、个人32位PC、win7系统
#include   <windows.h>   
#include   <stdio.h> 

DWORD nStartTime;
DWORD nThreadSwithTime;

DWORD CALLBACK   Thread1(PVOID   pvoid)  
{       
	while( 1) 
	{   
		Sleep(100);

		nStartTime = GetTickCount();	
	}     
 
	return   0;  
}


DWORD CALLBACK   Thread2(PVOID   pvoid)  
{       
	while(1) 
	{   
		nThreadSwithTime = (GetTickCount()-nStartTime);
		if (nThreadSwithTime > 0)
		{
			printf("nThread Swith Time = %d\n",nThreadSwithTime); 
		}
		
		
		Sleep(100);
	}     

	return   0;  
}

int    main()   
{      
	DWORD   dwThreadId;  
	DWORD   dwThread2Id;  
	nStartTime = GetTickCount();
	HANDLE   hThread1  =    CreateThread(NULL,0,Thread1,0,0,&dwThreadId);
	HANDLE   hThread2  =    CreateThread(NULL,0,Thread2,0,0,&dwThreadId);


	while (1)
	{
		Sleep(1000);
	}

}