首页 > 代码库 > 015 线程优先级
015 线程优先级
线程优先级
● 线程优先级
○ Windows的每一个可调度的线程分配了一个优先级(0-31),当系统准备去执行一条线程时,会首先看优先级为31的行程,并以循环的方式来进行 调度,只要有优先级为31的线程,操作系统将永远不会调用30以下的线程,这样看起来好像优先级较低的线 程永远得不到被执行的机会,但是其实系统当大部分线程都是属于不可调度状态的,譬如,当调用了GetMessage函数后,会导致线程休眠,从而变成了不可调度状态。
○ 还需要提出的一个问题是
○ 优先级较高的线程总是会抢占优先级较低线程的时间片,无论优先级较低的县城是否在执行属于自己的时间片,当较高优先级的线程已经混呗好可以运行的时候他会直接打断优先级较低的程线程的执行顺序,并将CPU时间片分配给优先级较高的线程。操作系统当 中有一个优先级为 0 的线程,为页面清零线程(Zero Page Thread),他负责在系统空闲时清理所有闲置的内存,将所有的闲置内存进行清零操作。
○ 线程优先级
因为系统设计的需要,Windows需要不停的切换进程或者选择某一进程进行运行,所以Windows对进程进行了优先级的设置。
real-time 实时
high 立即
above homal 较高
normal 正常
below nomal 较低
idle 低
○ 不应该有任何的进程运行在实时优先级以下,实时优先级可能会影响到操作i系统任务,可能会导致磁盘网络通信键盘鼠标等的使用。进程优先级,将会影响线程当中的优先级。
线程优先级 real-time hight above normal normal below normal idel
time-critical 31 15 15 15 15 15
highest 26 15 12 10 8 6
above normal 25 14 11 9 7 5
normal 24 13 10 8 6 4
below normal 23 12 9 7 5 3
lowest 22 11 8 6 4 2
idle 16 1 1 1 1 1
1 #define UNICODE 2 #include <windows.h> 3 #include <stdio.h> 4 5 int main() 6 { 7 SetProcessAffinityMask(GetCurrentProcess(), 0x1); 8 while(true) 9 { 10 printf("1"); 11 } 12 return 0; 13 }
○ 一号线程(CPU使用率非常高)
○ 用SetProcessAffinityMask为进程指定CPU
○ GetCurrentProcess 获得当前进程的句柄
1 he SetProcessAffinityMask function sets a processor affinity mask for the threads of the specified process. 2 3 BOOL SetProcessAffinityMask( 4 HANDLE hProcess, 5 DWORD_PTR dwProcessAffinityMask 6 );
1 HANDLE WINAPI GetCurrentProcess(void);
1 #define UNICODE 2 #include <windows.h> 3 #include <process.h> 4 #include <stdio.h> 5 6 int gNum1 = 0, gNum2 = 0, gNum3 = 0; 7 unsigned __stdcall ThreadFun1(void *lParam) 8 { 9 while(true) 10 { 11 gNum1 ++; 12 //printf("%d\r\n",(int)lParam); 13 } 14 return 0; 15 } 16 17 unsigned __stdcall ThreadFun2(void *lParam) 18 { 19 while(true) 20 { 21 gNum2++; 22 //printf("%d\r\n",(int)lParam); 23 } 24 return 0; 25 } 26 27 unsigned __stdcall ThreadFun3(void *lParam) 28 { 29 while(true) 30 { 31 gNum3++; 32 //printf("%d\r\n",(int)lParam); 33 } 34 return 0; 35 } 36 37 38 int main() 39 { 40 HANDLE hThreads[3] = { INVALID_HANDLE_VALUE }; 41 hThreads[0] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun1, (void*)1,CREATE_SUSPENDED,nullptr)); 42 SetThreadPriority(hThreads[0], THREAD_PRIORITY_TIME_CRITICAL); 43 ResumeThread(hThreads[0]); 44 45 hThreads[1] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun2, (void*)2,CREATE_SUSPENDED,nullptr)); 46 SetThreadPriority(hThreads[1], THREAD_PRIORITY_TIME_CRITICAL); 47 ResumeThread(hThreads[1]); 48 49 hThreads[2] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun3, (void*)3,CREATE_SUSPENDED,nullptr)); 50 SetThreadPriority(hThreads[2], THREAD_PRIORITY_IDLE); 51 ResumeThread(hThreads[2]); 52 53 Sleep(1000); 54 printf("gNum1:%d\r\ngNum2:%d\r\ngNum3:%d\r\n",gNum1,gNum2,gNum3); 55 56 WaitForMultipleObjects(3, hThreads, TRUE, INFINITE); 57 58 for(int i = 0; i<3; ++i) 59 { 60 CloseHandle(hThreads[i]); 61 } 62 63 return 0; 64 }
1 #define UNICODE 2 #include <windows.h> 3 #include <process.h> 4 #include <stdio.h> 5 6 int gNum1 = 0, gNum2 = 0, gNum3 = 0; 7 unsigned __stdcall ThreadFun1(void *lParam) 8 { 9 while(true) 10 { 11 gNum1 ++; 12 //printf("%d\r\n",(int)lParam); 13 } 14 return 0; 15 } 16 17 unsigned __stdcall ThreadFun2(void *lParam) 18 { 19 while(true) 20 { 21 gNum2++; 22 //printf("%d\r\n",(int)lParam); 23 } 24 return 0; 25 } 26 27 unsigned __stdcall ThreadFun3(void *lParam) 28 { 29 while(true) 30 { 31 gNum3++; 32 //printf("%d\r\n",(int)lParam); 33 } 34 return 0; 35 } 36 37 38 int main() 39 { 40 HANDLE hThreads[3] = { INVALID_HANDLE_VALUE }; 41 hThreads[0] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun1, (void*)1,CREATE_SUSPENDED,nullptr)); 42 SetThreadPriority(hThreads[0], THREAD_PRIORITY_TIME_CRITICAL); 43 ResumeThread(hThreads[0]); 44 45 hThreads[1] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun2, (void*)2,CREATE_SUSPENDED,nullptr)); 46 SetThreadPriority(hThreads[1], THREAD_PRIORITY_TIME_CRITICAL); 47 ResumeThread(hThreads[1]); 48 49 hThreads[2] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun3, (void*)3,CREATE_SUSPENDED,nullptr)); 50 SetThreadPriority(hThreads[2], THREAD_PRIORITY_IDLE); 51 ResumeThread(hThreads[2]); 52 53 while(true) 54 { 55 Sleep(100); 56 printf("gNum1:%d\r\ngNum2:%d\r\ngNum3:%d\r\n-----------------------------------\r\n",gNum1,gNum2,gNum3); 57 } 58 WaitForMultipleObjects(3, hThreads, TRUE, INFINITE); 59 60 for(int i = 0; i<3; ++i) 61 { 62 CloseHandle(hThreads[i]); 63 } 64 65 return 0; 66 }
1 #define UNICODE 2 #include <windows.h> 3 #include <process.h> 4 #include <stdio.h> 5 6 int gNum1 = 0, gNum2 = 0, gNum3 = 0; 7 unsigned __stdcall ThreadFun1(void *lParam) 8 { 9 while(true) 10 { 11 gNum1 ++; 12 //printf("%d\r\n",(int)lParam); 13 } 14 return 0; 15 } 16 17 unsigned __stdcall ThreadFun2(void *lParam) 18 { 19 while(true) 20 { 21 gNum2++; 22 //printf("%d\r\n",(int)lParam); 23 } 24 return 0; 25 } 26 27 unsigned __stdcall ThreadFun3(void *lParam) 28 { 29 while(true) 30 { 31 gNum3++; 32 //printf("%d\r\n",(int)lParam); 33 } 34 return 0; 35 } 36 37 38 int main() 39 { 40 SYSTEM_INFO system; 41 system.dwActiveProcessorMask;//1111111111 42 system.dwNumberOfProcessors; //8个核心 43 SetProcessAffinityMask(GetCurrentProcess(), 0x1); 44 HANDLE hThreads[3] = { INVALID_HANDLE_VALUE }; 45 hThreads[0] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun1, (void*)1,CREATE_SUSPENDED,nullptr)); 46 SetThreadPriority(hThreads[0], THREAD_PRIORITY_TIME_CRITICAL); 47 ResumeThread(hThreads[0]); 48 49 hThreads[1] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun2, (void*)2,CREATE_SUSPENDED,nullptr)); 50 SetThreadPriority(hThreads[1], THREAD_PRIORITY_TIME_CRITICAL); 51 ResumeThread(hThreads[1]); 52 53 hThreads[2] = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, ThreadFun3, (void*)3,CREATE_SUSPENDED,nullptr)); 54 SetThreadPriority(hThreads[2], THREAD_PRIORITY_IDLE); 55 ResumeThread(hThreads[2]); 56 57 while(true) 58 { 59 Sleep(100); 60 printf("gNum1:%d\r\ngNum2:%d\r\ngNum3:%d\r\n-----------------------------------\r\n",gNum1,gNum2,gNum3); 61 } 62 WaitForMultipleObjects(3, hThreads, TRUE, INFINITE); 63 64 for(int i = 0; i<3; ++i) 65 { 66 CloseHandle(hThreads[i]); 67 } 68 69 return 0; 70 }
015 线程优先级