I have a thread with a socket waiting for a message, but there is also another thread created from the previous one to terminate the first thread in case of not receiving a message in less than 5 seconds. How can I finish the first thread from the second?
// the second thread
void b(int t)
{
std::chrono::steady_clock::time_point currentTime =
std::chrono::high_resolution_clock::now();
while (1)
{
std::chrono::steady_clock::time_point n = std::chrono::high_resolution_clock::now();
int a = std::chrono::duration_cast<std::chrono::milliseconds>(n - currentTime).count();
if (a >= t)
{
std::cout << "time out" << std::endl;
break;
}
}
}
// the first thread
void a(Sock s)
{
std::thread(&b);
const char *msg[5000];
s.Read(msg[0], sizeof(msg));
std::cout << *msg << std::endl;
s.Send("MRS");
s.Close();
}