【Win32多线程】如何初始化一个新线程,停止一个执行中的线程,调整线程优先权?

来源:互联网 发布:java获取文件的后缀名 编辑:程序博客网 时间:2024/05/10 00:38
如何在某个线程内终止另一个正在运行的线程?

--------干净地终止一个线程
利用TerminateThread()安全的关闭执行中的一个线程
BOOL TerminateThread(
  HANDLE hThread,    // handle to thread
  DWORD dwExitCode   // exit code线程的结束代码
);

该函数会引来许多问题,如果内存泄露。尽量不使用TerminateThread()
下面利用event对象来干净的结束一个线程
/* *  * Demonstrates how to request threads to exit. * 结束线程 *  */#define WIN32_LEAN_AND_MEAN#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <time.h>DWORD WINAPI ThreadFunc(LPVOID);HANDLE hRequestExitEvent = FALSE;int main(){    HANDLE hThreads[2];    DWORD dwThreadId;    DWORD dwExitCode = 0;    int i;    hRequestExitEvent = CreateEvent(        NULL, TRUE, FALSE, NULL);//创建event对象,一开始处于未激发状态,靠程序来设置激发    for (i=0; i<2; i++)       hThreads[i] = CreateThread(NULL,            0,            ThreadFunc,            (LPVOID)i,            0,            &dwThreadId);           // Wait around for awhile, make    // sure the thread is running.    Sleep(1000);//保证线程可以运行    SetEvent(hRequestExitEvent);//设置event激发状态通知线程退出    WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);//主线程退出之前,确保其他线程已经安全退出    for (i=0; i<2; i++)        CloseHandle(hThreads[i]);    return EXIT_SUCCESS;}DWORD WINAPI ThreadFunc(LPVOID p){    int i;    int inside = 0;    UNREFERENCED_PARAMETER(p);    /* Seed the random-number generator */    srand( (unsigned)time( NULL ) );    for (i=0; i<1000000; i++)    {        double x = (double)(rand())/RAND_MAX;        double y = (double)(rand())/RAND_MAX;        if ( (x*x + y*y) <= 1.0 )            inside++;        if (WaitForSingleObject(hRequestExitEvent, 0) != WAIT_TIMEOUT) //等待event对象的激发状态        {            printf("Received request to terminate\n");  //当收到event激发,则退出            return (DWORD)-1;        }    }    printf("PI = %.4g\n", (double)inside / i * 4);    return 0;}

-----线程优先权(Thread Priority)

优先权类别:它是进程的属性之一。这个属性可以表现出这一个进程和其他进程比较之下的重要性。Win32提供4种优先权类别。每一个类别对应一个基本的优先权层级。
优先权类别                                  基础优先权值
HIGH_PRIORITY_CLASS                            13
IDLE_PRIORITY_CLASS                             4
NORMAL_PRIORITY_CLASS                           7or8(大部分程序使用的)
REALTIME_PRIORITY_CLASS                          24
优先权类别适用于进程而非线程。可以利用SetPriortiyClass()和GetPriorityClass()来调整和验证其值。


优先权层级:线程的优先权层级是对进程的优先权类别的一个修改,使得你能够调整同一个进程内的各个线程的相对重要性。一共有七种优先权层级。

优先级层级                                                         调整值
 HTREAD_PRIORITY_HIGHEST                      +2
HTREAD_PRIORITY_ABOVE_NORMAL          +1
HTREAD_PRIORITY_NORMAL                        0
HTREAD_PRIORITY_BELOW_NORMAL          -1
HTREAD_PRIORITY_LOWEST                        -2
HTREAD_PRIORITY_IDLE                                 set to 1
HTREAD_PRIORITY_TIME_CRITICAL                set to 15


优先权层级利用SetThreadPriority()改变。
BOOL SetThreadPriority(
  HANDLE hThread, // handle to the thread
  int nPriority   // thread priority level
);

获取目前线程的优先权GetThreadPriority()
int GetThreadPriority(
  HANDLE hThread   // handle to thread
);

创建一个线程,但是不要马上执行:

HANDLE hThread;DWORD threadId;hThread=CreateThread(NULL,0,THreadFunc,0,CREATE_SUSPENDED,&threadId);一旦线程设定妥当,可以调用ResumeThread()开始执行。DWORD ResumeThread(  HANDLE hThread   // handle to thread);

---挂起一个线程

SuspendThread()允许调用端指定一个线程睡眠。直到又有人调用了ResumeThread(),这个线程才会醒来。
DWORD SuspendThread(
  HANDLE hThread   // handle to thread
);
SuspendThread()的最大用途就是用来协调撰写调试器。调试器允许在程序的控制下,启动或停止任何一个线程。
复制搜索
原创粉丝点击