mardi 3 mars 2015

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

Could it be posible to cast or convert a std::thread thread in C++ to a HANDLE in Windows? I've been trying to manage threads in Windows with WINAPI functions for threads but I can't get it to work...



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


But seems to do nothing because thread keeps spawning "Hi's" in the console... Could there be a way to "kill" it using WINAPI?


Aucun commentaire:

Enregistrer un commentaire