Pthreads, how do I know that another thread inside the process is not waiting?

OS is Linux working with pthreads

I have two worker threads that run forever until the stop variable is true and the threads stop gracefully. Instead of waiting, both threads call pthread_cond_wait until the signal informs of a new task. The system is working well.

Please create a stream of information that will print some debugging information. The information stream will try to read and print information every 30 seconds. Part of this information I would like to be STATES of each work flow. Is it possible to determine if a thread is blocked in "pthread_cond_wait"? If the thread is expecting pthread_cond_wait, then STATE == waiting for else is done by STATE ==.

while ( (sharedvaluffer == 0) && (doneflag == 0) ) { pthread_cond_wait (&taks_added, &buffer); } 

Of course we can do this, we have more code. We can add to the above snippet a global variable that indicates that the thread is blocked. Code can be executed

 while ( (sharedvaluffer == 0) && (doneflag == 0) ) { lock; i_am_waiting = truel unlock pthread_cond_wait (&taks_added, &buffer); } 

The question is whether there is an easier way to scale. Stack of pending thread

 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 
+8
c linux pthreads
source share
4 answers

You can use the pthread_self() return as an identifier for owning a mutex. Store and compare. Since a mutex can be received by one thread at once, you will know which thread is working (not waiting) and which are not.

0
source share

You can register the general structure s in the mutex with pthread_mutexattr_getpshared , where each thread registers its state (running, not running) with pthread_self() .

Before checking the condition variable, you can set s[pthread_self()]->state = WAITING , and after checking you can set s[pthread_self()]->state = WORKING .

Be sure to create a structure, for example, no race condition will occur.

+1
source share

I would go the simple way and just include state enumeration in the stream. Before changing each conceptual state, you would change the state.

 void worker(void* parm) { threadstate_t *state = (threadstate_t*)parm; /* ... */ while (...) { state->current = STATE_WORKING; /* ... */ state->current = STATE_WAITING; /* res = pthread_cond_wait(cond, mutex); */ } } 

Then in your poll thread:

 void worker_dbg(void* parm) { threadstate_t *states = (threadstate_t*)parm; int i; while (run) { for (i = 0; i < NWORKERS; ++i) { /* _state is a map of states to strings */ printf("Thread %d: %s\n", states[i].id, _state[states[i].current]); } sleep(30); } } 

If you are disconnected for 30 seconds because the status was updated immediately after printing, it does not really matter. You do not need to block the state, since you are only writing from the owner, and you are only reading from the debug stream.

+1
source share

Presumably, you are using a mutex that is blocked when the condition is notified, if so, just try to block this mutex in your information flow - if you purchased it, it’s good that while you tried it, the stream is waiting, otherwise, if it is blocked, then you know that the thread does this thing (i.e., it acquired this mutex, which means its critical section in it)

0
source share

All Articles