挂起线程
线程挂起的个数由宏 MAXIMUM_SUSPEND_COUNT 决定。
线程挂起之后 就进入 阻塞态
线程可以连续多次挂起,既深层次挂起。挂起多少次就得恢复多少次。
注意:挂起当前线程时,如果存在堆操作,可能就会把堆锁死,其他线程就不能操作该堆。
DWORD WINAPI SuspendThread(
__in HANDLE hThread
);
挂起之后恢复
恢复后就进入 就绪态
DWORD WINAPI ResumeThread(
__in HANDLE hThread
);
线程睡眠
VOID WINAPI Sleep(
__in DWORD dwMilliseconds //毫秒
);
执行之后线程进入阻塞态,睡眠时间之后进入就绪态
INFINITE : 永远进入阻塞状态
线程切换
BOOL WINAPI SwitchToThread(void);
该函数的功能是把自己当前的时间片放弃,交给操作系统处理优先级更低的线程
就绪态进入运行态,由操作系统调用决定
获取线程上下文(现场)
BOOL WINAPI GetThreadContext(
__in HANDLE hThread,
__in_out LPCONTEXT lpContext
);
typedef struct _CONTEXT {
...
} CONTEXT, *LPCONTEXT;
该结构体包含由处理器寄存器数据
获取线程现场步骤
CONTEXT &Context;
SuspendThread(hThread)
GetThreadContext(hThread, &Context)
例程1:线程挂起与恢复
#include
#include
#include
DWORD WINAPI ThreadProFunc(LPVOID lpParam);
int main(int argc, char **argv)
{
int suspendCount = 0;
int resumSuspendCount = 0;
HANDLE hThread;
DWORD dwThreadId;
hThread = CreateThread( NULL, NULL, ThreadProFunc, NULL, 0, &dwThreadId);
suspendCount = SuspendThread(hThread); //挂起线程,返回线程的前一次挂起次数
printf("suspendCount = %d\n", suspendCount);
suspendCount = SuspendThread(hThread);
printf("suspendCount = %d\n", suspendCount);
suspendCount = SuspendThread(hThread);
suspendCount = SuspendThread(hThread);
printf("suspendCount = %d\n", suspendCount);
// 恢复挂起的线程
for(int i = 0; i <= suspendCount; i++) {
resumSuspendCount = ResumeThread(hThread); //返回恢复前一次挂起的值,恢复之前挂起四次
printf("resumSuspendCount = %d\n", resumSuspendCount);
}
CloseHandle(hThread); //关闭线程句柄
system("pause");
return 0;
}
DWORD WINAPI ThreadProFunc(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("hello\n");
}
return 0;
}
运行结果
suspendCount = 0
suspendCount = 1
suspendCount = 3
resumSuspendCount = 4
resumSuspendCount = 3
resumSuspendCount = 2
resumSuspendCount = 1
hello
hello
hello
hello
请按任意键继续. . .
例程2:线程睡眠切换
#include
#include
#include
DWORD WINAPI ThreadProFuncA(LPVOID lpParam);
DWORD WINAPI ThreadProFuncB(LPVOID lpParam);
int main(int argc, char **argv)
{
HANDLE hThreadA;
HANDLE hThreadB;
DWORD dwThreadIdA;
DWORD dwThreadIdB;
hThreadA = CreateThread( NULL, NULL, ThreadProFuncA, NULL, 0, &dwThreadIdA);
hThreadB = CreateThread( NULL, NULL, ThreadProFuncB, NULL, 0, &dwThreadIdB);
CloseHandle(hThreadA);
CloseHandle(hThreadB);
system("pause");
return 0;
}
DWORD WINAPI ThreadProFuncA(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("hello\n");
Sleep(500);
}
return 0;
}
DWORD WINAPI ThreadProFuncB(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("world\n");
Sleep(500);
}
return 0;
}
运行结果
hello
world
请按任意键继续. . . hello
world
hello
world
hello
world
例程3:不同优先级,线程睡眠切换不了
#include
#include
#include
DWORD WINAPI ThreadProFuncA(LPVOID lpParam);
DWORD WINAPI ThreadProFuncB(LPVOID lpParam);
int main(int argc, char **argv)
{
HANDLE hThreadA;
HANDLE hThreadB;
DWORD dwThreadIdA;
DWORD dwThreadIdB;
hThreadA = CreateThread( NULL, NULL, ThreadProFuncA, NULL, 0, &dwThreadIdA);
SetThreadPriority(hThreadA, THREAD_PRIORITY_HIGHEST);
hThreadB = CreateThread( NULL, NULL, ThreadProFuncB, NULL, 0, &dwThreadIdB);
SetThreadPriority(hThreadB, THREAD_PRIORITY_LOWEST);
CloseHandle(hThreadA);
CloseHandle(hThreadB);
system("pause");
return 0;
}
DWORD WINAPI ThreadProFuncA(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("hello\n");
Sleep(0); // 同级别优先级切换
}
return 0;
}
DWORD WINAPI ThreadProFuncB(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("world\n");
Sleep(0);
}
return 0;
}
运行结果
hello
hello
hello
hello
world
world
world
world
请按任意键继续. . .
例程4:不同优先级,线程切换
#include
#include
#include
DWORD WINAPI ThreadProFuncA(LPVOID lpParam);
DWORD WINAPI ThreadProFuncB(LPVOID lpParam);
int main(int argc, char **argv)
{
HANDLE hThreadA;
HANDLE hThreadB;
DWORD dwThreadIdA;
DWORD dwThreadIdB;
hThreadA = CreateThread( NULL, NULL, ThreadProFuncA, NULL, 0, &dwThreadIdA);
SetThreadPriority(hThreadA, THREAD_PRIORITY_HIGHEST);
hThreadB = CreateThread( NULL, NULL, ThreadProFuncB, NULL, 0, &dwThreadIdB);
SetThreadPriority(hThreadB, THREAD_PRIORITY_LOWEST);
CloseHandle(hThreadA);
CloseHandle(hThreadB);
system("pause");
return 0;
}
DWORD WINAPI ThreadProFuncA(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("hello\n");
SwitchToThread(); // 线程可以切换到低优先级
}
return 0;
}
DWORD WINAPI ThreadProFuncB(LPVOID lpParam)
{
for(int i = 0; i < 4; i++) {
printf("world\n");
SwitchToThread();
}
return 0;
}
运行结果
hello
hello
hello
world
hello
world
world
world
请按任意键继续. . .