Windows threads: scheduling, priority, cpu relevance

Thread scheduling

Thread suspend and resume

There is a count value representing pending thread suspend count is initialized to 1. Create a thread, it will not be scheduled in CPU kernel object thread, when hangover count is 0, the thread may be scheduled.

DWORD ResumeThread(HANDLE hThread);	//改变线程为可调度

Success: pending before a count of
failure to return: 0xFFFFFFFF

DWORD SuspendThread(HANDLE hThread);	//挂起线程

Suspended many times, how many times must be restored, the system will allocate CPU that thread
suspend +1, -1 recovery, the hangover count is 0 when only dispatched

Sleeping

When not scheduled, you can call:

VOID Sleep (DWORD dwMilliseconds);
将线程挂起dwMilliseconds长的时间(单位为毫秒,INFINITE为永远不要进行调度)

Hyper-Threading (Xeon): Pentium4 and update of a technology 1CPU support, Hyper-Threading processor chip has multiple "logical" CPU, each of which can run a thread, each thread has its own architectural state.

Time for a thread of execution

BOOL GetThreadTimes();
//高精度计时
BOOL QueryPerformanceFrequency(LARGE_INTEGER* pliFrequency);
BOOL QueryPerformanceCounter(LARGE_INTEGER* pliCount);

Thread priority

Scheduler allocating CPU, a thread scheduling may be performed in accordance with the scheduling priority.
Each thread is assigned 0 (lowest) to 31 (highest) priority, round-robin fashion to find the thread from 31-0.
When the high-priority thread occupy CPU, resulting in lower priority thread can not run, a condition called starvation. The possibility of starvation on multiprocessor machines happen to be much smaller.
Problem: Windows is a preemptive, when the low-priority execution, the high priority thread is ready, it will suspend the implementation of high priority low priority thread.

The process can never dispatch, scheduling can be threads, processes just an abstract concept proposed by Microsoft, in general few high-priority thread scheduling, because they can quickly get CPU, contrast, lower priority should be to maintain scheduling state, using a lot of CPU time.

Thread priority

Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description

When you create a thread, the priority is set to normal, can be changed after creation

//设置线程优先级
BOOL SetThreadPriority(
	HANDLE hThread,	//线程句柄
	int nPriority);			//相对线程优先级
//获取线程的相对优先级
int GetThreadPriority(HANDLE hThread);
//首先创建挂起,修改后启动线程,再关闭线程句柄
DWORD dwThreadID;
HANDLE hThread = CreateThread(NULL,0,ThreadFunc,NULL,
	CREATE_SUSPENDED,&dwThreadID);
	SetThreadPriority(hThread,THREAD_PRIORITY_IDLE);
	ResumeThread(hThread);
	CloseHandle(hThread);

Relevance

Relevance about the meaning refers to the hardware associated with the
thread running on a processor on the first run, so that the thread always runs on the same processor helps reuse data is still processor cache

NUMA (non-uniform memory access) computer architecture: a plurality of system boards, each board has its own system CPU and memory blocks.
Windows Vista allows us to set up the association process and thread, the thread can control hard associated with the CPU.

//获取计算机CPU数量:
GetSystemInfo();
//限制线程只能在固定CPU上运行:
BOOL SetThreadAffinityMask(
	HANDLE hThread,	//线程句柄
	DWORD_PTR dwThreadAffinityMask);	//指定CPU
例如:
SetThreadAffinityMask(hThread1,0x00000001);
//灵活使用CPU函数,分配CPU后可以灵活调整:
DWORD SetThreadIdealProcessor(
	HANDLE hThread,		//线程句柄
	DWORD dwIdealProcessor);		//一般传入MAXIMUM_PROCESSORS,表示线程没有理想的CPU使用

Guess you like

Origin blog.csdn.net/qq_42856154/article/details/90752445