Anerudhan Gopal
Anerudhan Gopal

Reputation: 389

monitor the status of a thread

Is the following piece of code

  if(ESRCH == pthread_kill(Thread_Id,0))
            start_Thread_Again

a good way to check if my thread has accidentally exited? Can we use it similar to

   wait(-1,W_NOHANG)

Upvotes: 1

Views: 229

Answers (2)

David Schwartz
David Schwartz

Reputation: 182761

It's hard to be sure because of how little context there is in your question. But it's indicative of a person who is thinking about threads in a completely incorrect way.

There is no reason a piece of typical application code should ever care about what another thread specifically is doing. You may care about the status of some work that is being done, but that concern should be logically independent of what thread may or may not be doing that work at that time.

If your question is whether some work is finished or not or whether or not some some task is being worked on, looked at structures associated with that work or task. Those structures wouldn't be associated with a particular thread but with something that needed to get done.

Upvotes: 2

zvrba
zvrba

Reputation: 24546

"Accidentally exited?" A thread does not exit accidentally, it exits because your code tells it to do so [meaning you have a bug if you didn't want it to exit].

From the manpage for pthread_kill: "As in kill(), if sig is zero, error checking shall be performed but no signal shall actually be sent."

Your code is wrong, it must check for equality.

Upvotes: 4

Related Questions