OS is Linux, working with pthreads
I have two worker threads that run forever, until a stop variable takes value true, and the threads terminate gracefully. Instead of doing busy waiting both threads call pthread_cond_wait until a signal notifies for a new task. The system works well.
It is requested to create an "info" thread that will print some debugging information. The info thread will try to read and print information every 30 seconds.Part of this info, I would like to be the STATE of each worker thread. Is it possible to find if a thread is blocked in "pthread_cond_wait"? If the thread waits is pthread_cond_wait then STATE==waiting else the STATE==running.
while ( (sharedvaluffer == 0) && (doneflag == 0) ) {
pthread_cond_wait (&taks_added, &buffer);
}
Of course we can do that we more code. We can add to the above snippet a global variable that marks that thread as locked. The code can be done
while ( (sharedvaluffer == 0) && (doneflag == 0) ) {
lock;
i_am_waiting = truel
unlock
pthread_cond_wait (&taks_added, &buffer);
}
The question is, if there is an easier more scalable way. The stack of a waiting thread is
Thread 6 (Thread 0x40800940 (LWP 20732)):
#0 0x00002ba4567a9326 in pthread_cond_wait@@GLIBC_2.3.2 ()
#1 0x00000000007ce2ed in worker(void*) ()
#2 0x00002ba4567a5193 in start_thread () from /lib64/libpthread.so.0
#3 0x00002ba458a82f0d in clone () from /lib64/libc.so.6