简体   繁体   English

pthreads,我怎么知道进程内的另一个线程不在等待?

[英]pthreads, how do I know that another thread inside the process is not waiting?

OS is Linux, working with pthreads 操作系统是Linux,使用pthreads

I have two worker threads that run forever, until a stop variable takes value true, and the threads terminate gracefully. 我有两个永远运行的工作线程,直到stop变量的值为true,并且线程正常终止为止。 Instead of doing busy waiting both threads call pthread_cond_wait until a signal notifies for a new task. 而不是忙于等待,两个线程都调用pthread_cond_wait,直到信号通知新任务为止。 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. info线程将尝试每30秒读取和打印一次信息。作为此信息的一部分,我想成为每个工作线程的STATE。 Is it possible to find if a thread is blocked in "pthread_cond_wait"? 是否可以找到“ pthread_cond_wait”中是否阻塞了线程? If the thread waits is pthread_cond_wait then STATE==waiting else the STATE==running. 如果线程等待的是pthread_cond_wait,则STATE ==正在等待,否则STATE ==正在运行。

 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

You could register a shared structure s in the mutex with pthread_mutexattr_getpshared , where each thread registers its state (running, not running) with the aid of pthread_self() . 您可以在互斥量中使用pthread_mutexattr_getpshared注册一个共享结构s ,其中每个线程借助pthread_self()注册其状态(运行中,未运行pthread_self()

Before checking the condition variable, you can set s[pthread_self()]->state = WAITING , and after the check you can set s[pthread_self()]->state = WORKING . 在检查条件变量之前,可以设置s[pthread_self()]->state = WAITING ,在检查之后可以设置s[pthread_self()]->state = WORKING

Be sure to design the structure such as no race condition will occur. 确保设计结构,例如不会发生竞争状况。

I would go the simple route and just include a state enum per thread. 我会走简单的路线,只在每个线程中包含一个状态枚举。 Prior to each conceptual state change you would alter 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 interrogration 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're off by 30 seconds because the state got updated right after you printed, it doesn't really matter. 如果你了30秒钟,因为状态得到了更新你的印刷之后 ,它并不真正的问题。 No need to lock on the state as you only write from the owning worker and you only read from the debug thread. 无需锁定状态,因为您仅从拥有者的工作人员进行写入,而仅从调试线程进行读取。

大概您正在使用一个互斥锁,当条件通知时,该互斥锁会被锁定;如果是这样,则在您的信息线程中,只需尝试锁定该互斥锁-如果您获取了该互斥锁,那么很可能在采样时线程正在等待,否则如果它阻塞了,那么您就知道线程正在执行此操作(即它已经获取了该互斥锁,这意味着它位于关键部分)

You could use the return of pthread_self() as identifier for the mutex ownership. 您可以将pthread_self()的返回值用作互斥体所有权的标识符。 Store and compare. 存储和比较。 Since a mutex can be acquired by a single thread at once, you will know which thread is running (not waiting), and which are not. 由于一个线程可以同时获取一个互斥锁,因此您将知道哪个线程正在运行(不等待),而哪个线程不在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM