C++线程优先级SetThreadPriority的使用实例
时间:2021-05-10 08:49:34|栏目: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++程序设计有所帮助。
栏 目:C代码
下一篇:C语言对组文件处理的相关函数小结
本文标题:C++线程优先级SetThreadPriority的使用实例
本文地址:http://www.codeinn.net/misctech/118856.html