Buğra Gedik
Buğra Gedik

Reputation: 724

Detecting scheduled threads on linux

I need to detect whether a given thread is currently scheduled to a CPU or not. I am on a linux system with pthreads. Say that my code is running on a signal handler and I know that there are threads X and Y in my app, and from within my signal handler I want to check if X and Y are currently scheduled or not.

Thanks for your help.

Upvotes: 0

Views: 332

Answers (1)

Seth Robertson
Seth Robertson

Reputation: 31461

In general if you have to ask this question, you are probably thinking incorrectly. Whatever answer you are going to get is going to be immediately outdated and whatever you plan to do about the answer is going to be subject to massive race conditions.

However, in theory you can look at /proc/getpid()/task/threadid/status and find out if a particular thread is running or not. Note this is very much a linux thing and any mapping between "threadid" and pthread_self()'s return code or pthread_create's pthread_t copy-out is very much not a standard.

Upvotes: 3

Related Questions