ResumeThread用于一个已结束的线程

概要

如果一个线程已经运行完了,再次用于ResumeThread()会怎么样?答案是:不怎么样,只是什么也不干而已。

ResumeThread是把之前suspend的线程resume起来,如果该线程没有被suspend,自然也无resume之说。

MSDN的说明

ResumeThread

The ResumeThread function decrements a thread’s suspend count. When the suspend count is decremented to zero, the execution of the thread is resumed.

DWORD ResumeThread(
  HANDLE hThread
);

Parameters

hThread

[in] Handle to the thread to be restarted.
The handle must have the THREAD_SUSPEND_RESUME access right. For more information, see Thread Security and Access Rights.

Return Values

If the function succeeds, the return value is the thread’s previous suspend count.

If the function fails, the return value is (DWORD) -1. To get extended error information, call GetLastError.

Remarks

The ResumeThread function checks the suspend count of the subject thread. If the suspend count is zero, the thread is not currently suspended. Otherwise, the subject thread’s suspend count is decremented. If the resulting value is zero, then the execution of the subject thread is resumed.

If the return value is zero, the specified thread was not suspended. If the return value is 1, the specified thread was suspended but was restarted. If the return value is greater than 1, the specified thread is still suspended.

Note that while reporting debug events, all threads within the reporting process are frozen. Debuggers are expected to use the SuspendThread and ResumeThread functions to limit the set of threads that can execute within a process. By suspending all threads in a process except for the one reporting a debug event, it is possible to “single step” a single thread. The other threads are not released by a continue operation if they are suspended.

示例一

创建一个线程,线程自动执行,等待线程结束,Resume。

代码

#include "stdafx.h"
#include <Windows.h>
#include <math.h>

DWORD WINAPI Calc(LPVOID p)
{
    for (int i = 0; i < 500; i++) {
        printf("sqrt(%d)=%.4lf\n", i, sqrt((double)i));
    }

    return 0;
}

void TestThread()
{
    HANDLE hThread = CreateThread(NULL, 0, Calc, NULL, 0, NULL);
    WaitForSingleObject(hThread, INFINITE);

    printf("ResumeThread...\n");
    DWORD dwRet = ResumeThread(hThread);
    printf("dwRet=%d\n", dwRet);
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);
}

int _tmain(int argc, _TCHAR* argv[])
{
    TestThread();

    return 0;
}

运行结果

sqrt(0)=0.0000
sqrt(1)=1.0000
...
sqrt(499)=22.3383
ResumeThread...
dwRet=0
请按任意键继续. . .

示例二

创建一个线程,不自动执行,suspend两次,再分别Resume。

下面的例子关注ResumeThread的返回值。

代码

void TestThread2()
{
    HANDLE hThread = CreateThread(NULL, 0, Calc, NULL, CREATE_SUSPENDED, NULL);
    DWORD dwRet = ResumeThread(hThread);
    printf("dwRet=%d\n", dwRet);
    WaitForSingleObject(hThread, INFINITE);

    printf("ResumeThread...\n");
    dwRet = ResumeThread(hThread);
    printf("dwRet=%d\n", dwRet);
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);
}

执行结果

dwRet=1
sqrt(0)=0.0000
sqrt(1)=1.0000
...
sqrt(499)=22.3383
ResumeThread...
dwRet=0
请按任意键继续. . .

示例3

多次Suspend、多次Resume。

代码

void TestThread3()
{
    DWORD dwRet;
    HANDLE hThread = CreateThread(NULL, 0, Calc, NULL, CREATE_SUSPENDED, NULL);
    dwRet = SuspendThread(hThread);
    printf("SuspendThread(), dwRet=%d\n", dwRet);

    dwRet = ResumeThread(hThread);
    printf("ResumeThread(), dwRet=%d\n", dwRet);
    printf("WaitForSingleObject(): %d\n", WaitForSingleObject(hThread, 5000));

    dwRet = ResumeThread(hThread);
    printf("ResumeThread(), dwRet=%d\n", dwRet);
    printf("WaitForSingleObject(): %d\n", WaitForSingleObject(hThread, 5000));

    printf("ResumeThread...\n");
    dwRet = ResumeThread(hThread);
    printf("dwRet=%d\n", dwRet);
    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);
}

运行结果

SuspendThread(), dwRet=1 //返回之前的suspend个数
ResumeThread(), dwRet=2
WaitForSingleObject(): 258 //超时,suspend为2,所以必须两次resume才能执行
ResumeThread(), dwRet=1 //resume了两次,开始执行线程
sqrt(0)=0.0000
sqrt(1)=1.0000
...
sqrt(499)=22.3383
WaitForSingleObject(): 0
ResumeThread...
dwRet=0
请按任意键继续. . .

其中WaitFor返回值:

  • WAIT_ABANDONED=0x00000080L:The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.
  • WAIT_OBJECT_0=0x00000000L:The state of the specified object is signaled.
  • WAIT_TIMEOUT=0x00000102L(十进制258):The time-out interval elapsed, and the object’s state is nonsignaled.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值