c++ - 将 std::thread 转换为 HANDLE 以调用 TerminateThread()

标签 c++ windows multithreading winapi c++11

<分区>

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

#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;
}

但似乎什么也没做,因为线程不断在控制台中生成“Hi's”...是否有办法使用 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 thread\n";
            }
            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 thread\n";
            }
            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 thread\n";
            }
            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...

关于c++ - 将 std::thread 转换为 HANDLE 以调用 TerminateThread(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28845729/

相关文章:

java - 限制 JVM 使用的线程数

c++ - 如何检查数组是否包含多个元素?

c++ - 使用 for 循环的矩阵乘法会降低性能吗?

c++ - 方法返回值地址

c++ - 为什么当客户端只关闭发送一半的连接时,HTTP 服务器就关闭连接?

windows - 我是如何在我的应用程序中获得这个隐藏的 "URL Moniker Notification Window"的?

windows - 如何在 Windows Ubuntu 子系统中安装 substed 驱动器

c++ - C++低延迟线程异步缓冲流(打算记录)– Boost

Android:哪个线程调用.onSensorChanged?

c++ - 在某些情况下所需的假设声明中的名称查找规则是什么