将std::thread强制转换为HANDLE以调用TerminateThread()

Cast std::thread as HANDLE to call TerminateThread()

本文关键字:调用 TerminateThread HANDLE 转换 thread std      更新时间:2023-10-16

是否可以将C++中的std::thread线程强制转换或转换为Windows中的HANDLE?我一直在尝试在Windows中使用线程的WINAPI函数来管理线程,但我无法使其工作。。。

#include <thread>
#include <string>
#include <iostream>
#include <windows.h>
void Hi(std::string n){
while(true){
    std::cout<<"Hi :3 "<<n<<"n";
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
int main(void){
std::thread first(Hi, "zoditu");
first.detach();
getc(stdin);
//SuspendThread((void*)first.native_handle());
TerminateThread((void*)first.native_handle(), (unsigned long)0x00);
CloseHandle((void*)first.native_handle());
std::cout<<"No D:!!n";
getc(stdin);
return 0;
}

但似乎什么也没做,因为线程在控制台中不断生成"嗨"。。。有没有办法用WINAPI"杀死"它?

我认为将std::thread::native_handle()返回的值直接用于Win32 API函数没有任何错误(即,不需要转换)。

以下程序适用于我。但是,如果线程在活动执行时被终止,它通常(总是?)会崩溃,但如果线程在终止前被挂起,它会正常工作。正如您所知和其他人所指出的,终止线程通常不是一个好主意。

但是要回答您的问题Win32 API似乎在没有任何额外转换的情况下按预期工作。以下程序适用于我。

程序:

#include <windows.h>
#include <iostream>
#include <string>
#include <thread>
void foo()
{
    while (true)
    {
        std::cout << "foo()n";
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}
int main(void)
{
    std::thread first(foo);
    bool isFinished = false;
    while (!isFinished)
    {
        char ch = ::getchar();
        ::getchar(); // Swallow the new line character
        if (ch == 'e')
        {
            isFinished = true;
        }
        else if (ch == 's')
        {
            DWORD result = ::SuspendThread(first.native_handle());
            if (result != -1)
            {
                std::cout << "Successfully suspended threadn";
            }
            else
            {
                std::cout << "Failed to suspend thread: failure resson " << ::GetLastError() << "n";
            }
        }
        else if (ch == 'r')
        {
            DWORD result = ::ResumeThread(first.native_handle());
            if (result != -1)
            {
                std::cout << "Successfully resumed threadn";
            }
            else
            {
                std::cout << "Failed to resume thread: failure resson " << ::GetLastError() << "n";
            }
        }
        else if (ch == 'k')
        {
            DWORD result = ::TerminateThread(first.native_handle(), 1);
            if (result != 0)
            {
                std::cout << "Successfully terminated threadn";
            }
            else
            {
                std::cout << "Failed to terminate thread: failure resson " << ::GetLastError() << "n";
            }
        }
        else
        {
            std::cout << "Unhandled char '" << ch << "'n";
        }
    }
    first.detach();
    std::cout << "waiting to exit main...";
    ::getchar();
    std::cout << "exiting...n";
    return 0;
}

示例输出(我添加的注释):

foo()
foo()
foo()
foo()
s
Successfully suspended thread // This was successful since 'foo()' is no longer printing
r
Successfully resumed thread // This was successful since 'foo()' is again printing
foo()
foo()
foo()
foo()
s
Successfully suspended thread // Worked again
k
Successfully terminated thread // Says it works...
r
Successfully resumed thread // Termination must have worked because resuming did not cause 'foo' to start printing
e
waiting to exit main...
exiting...