C++线程优先级SetThreadPriority的使用实例
投稿:shichen2014
这篇文章主要介绍了C++线程优先级SetThreadPriority的使用实例,较为详细的讲述了C++线程及其优先级的用法,需要的朋友可以参考下
本文实例讲述了C++线程优先级SetThreadPriority的使用方法,分享给大家供大家参考。具体方法如下:
复制代码 代码如下:
// ThreadPriority.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
DWORD WINAPI ThreadProcIdle(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread IDLE...\n");
}
return 0;
}
DWORD WINAPI ThreadProcNormal(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread Normal...\n");
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThreadIdIdle;
DWORD dwThreadIdNormal;
HANDLE hThread[2];
//开启两个线程
hThread[0] = ::CreateThread(NULL,0, ThreadProcIdle, NULL, CREATE_SUSPENDED, &dwThreadIdIdle);
::SetThreadPriority(hThread[0],THREAD_PRIORITY_IDLE);
::ResumeThread(hThread[0]);
hThread[1] = ::CreateThread(NULL,0, ThreadProcNormal, NULL, CREATE_SUSPENDED, &dwThreadIdNormal);
::SetThreadPriority(hThread[1],THREAD_PRIORITY_NORMAL);
::ResumeThread(hThread[1]);
//等待两个线程结束
::WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
::CloseHandle(hThread[0]);
::CloseHandle(hThread[1]);
return 0;
}
//
#include "stdafx.h"
#include <Windows.h>
DWORD WINAPI ThreadProcIdle(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread IDLE...\n");
}
return 0;
}
DWORD WINAPI ThreadProcNormal(LPVOID lpParameter)
{
for (int i=0;i<20;i++)
{
printf("I'm in thread Normal...\n");
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwThreadIdIdle;
DWORD dwThreadIdNormal;
HANDLE hThread[2];
//开启两个线程
hThread[0] = ::CreateThread(NULL,0, ThreadProcIdle, NULL, CREATE_SUSPENDED, &dwThreadIdIdle);
::SetThreadPriority(hThread[0],THREAD_PRIORITY_IDLE);
::ResumeThread(hThread[0]);
hThread[1] = ::CreateThread(NULL,0, ThreadProcNormal, NULL, CREATE_SUSPENDED, &dwThreadIdNormal);
::SetThreadPriority(hThread[1],THREAD_PRIORITY_NORMAL);
::ResumeThread(hThread[1]);
//等待两个线程结束
::WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
::CloseHandle(hThread[0]);
::CloseHandle(hThread[1]);
return 0;
}
希望本文所述对大家的C++程序设计有所帮助。