将std::thread强制转换为HANDLE以调用TerminateThread()
Cast std::thread as HANDLE to call TerminateThread()
是否可以将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...
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 如何用参数值调用函数(仅在运行时已知)
- std::cout.imbue()多重调用
- 函数何时会在c++中包含stack_Unwind_Resume调用