Terry Li
Terry Li

Reputation: 17268

Given a thread id, how to decide it's alive or not in C++ on linux

Given only thread id, is it possible to decide from there whether the thread is still alive or not? I'm talking about C++ on linux.

Upvotes: 1

Views: 1722

Answers (1)

Useless
Useless

Reputation: 67733

From the manpage:

DESCRIPTION

   The pthread_kill() function shall request that a signal  be  deliv-
   ered to the specified thread.

   As in kill(), if sig is zero, error checking shall be performed but
   no signal shall actually be sent.

so:

bool isalive(int threadid)
{
  return pthread_kill(threadid, 0) != ESRCH;
}

Upvotes: 5

Related Questions