1. 程式人生 > >015 線程優先級

015 線程優先級

main 時間片 nor 機會 mas 一號線 他會 磁盤 resume

線程優先級

線程優先級
  ○ 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 線程優先級