i create class bassed thread, i want thread cancel and class be destroyed
//class
class HandShakeThreadClass
{
public:
HandShakeThreadClass(SOCKET sock) : sock(sock)
{
_data = "";
StartInternalThread();
}
~HandShakeThreadClass()
{
closesocket(sock);
pthread_cancel(_thread);
(void)pthread_join(_thread, NULL);
return;
}
/** Returns true if the thread was successfully started, false if there was an error starting the thread */
bool StartInternalThread()
{
return (pthread_create(&_thread, NULL, InternalThreadEntryFunc, this) == 0);
}
protected:
virtual void InternalThreadEntry()
{
int buffLength = 512;
std::vector<char> buffer(buffLength);
int socket_res;
bool is_active = true;
// u_long iMode = 1; !
//ioctlsocket(sock, FIONBIO, &iMode);//set client socket to none blocking
int ReceiveTimeout = 5000;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&ReceiveTimeout, sizeof(ReceiveTimeout));
do
{
printf("Repeat socket recv \n");
socket_res = recv(sock, &buffer[0], buffer.size(), 0);
if (socket_res == SOCKET_ERROR)
{
//
is_active = false;
continue;
}
else
{
//
}
continue;
} while (is_active);
// i want to cancle thread and delete this class !
this->~HandShakeThreadClass();
return;
};
private:
static void* InternalThreadEntryFunc(void* This)
{
((HandShakeThreadClass*)This)->InternalThreadEntry();
return NULL;
}
pthread_t _thread;
};
#endif