本文主要是介绍容易歧义的线程函数SuspendThread、ResumeThread 和如何获知线程是否还在运行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// Win32Thread.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include <stdio.h>
#include <windows.h>DWORD WINAPI TreadFunc(LPVOID lpParam)
{int i = 0;while (i < 20){printf("I am from a thread, count = %d\n", i++);}return 0;
}int _tmain(int argc, _TCHAR* argv[])
{HANDLE hTread = NULL;DWORD dwThreadId = 0;hTread = ::CreateThread(NULL, 0, TreadFunc, NULL, 0, &dwThreadId);printf("Now another thread has been created. ID=%d\n", dwThreadId);::SuspendThread(hTread);::SuspendThread(hTread);getchar();::ResumeThread(hTread);getchar();::ResumeThread(hTread);DWORD dwExitCode;if (::GetExitCodeThread(hTread, &dwExitCode)){if (dwExitCode == STILL_ACTIVE){printf("thread still active\n");}else{printf("thread exit.\n");}}::WaitForSingleObject(hTread, INFINITE);if (::GetExitCodeThread(hTread, &dwExitCode)){if (dwExitCode == STILL_ACTIVE){printf("thread still active\n");}else{printf("thread exit.\n");}}::CloseHandle(hTread);getchar();return 0;
}
SuspendThread:线程暂停计数 + 1
ResumeThread:线程暂停计数 - 1,当减为0的时候,线程有机会跑
GetExitCodeThread函数可以查看线程是否还在跑。
这篇关于容易歧义的线程函数SuspendThread、ResumeThread 和如何获知线程是否还在运行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!