检查线程状态,同时使其处于等待状态
我想知道是否可以检查线程的状态,该线程可能处于可等待状态,但不一定如此,如果它处于可等待状态,我想将其保留在该状态。
基本上,如何在不更改线程(可等待)状态的情况下检查线程的状态。
通过等待,我的意思是如果我调用 wait(pid) 它会正确返回并且不会挂起。
我还要补充一点,我正在跟踪一个多线程程序,因此我无法更改它的代码。另外,我也省略了这个信息,但这是一个基于 Linux 的系统。
I am wondering if it is possible to check the status of a thread, which could possibly be in a waitable state but doesn't have to be and if it is in a waitable state I would like to leave it in that state.
Basically, how can I check the status of a thread without changing its (waitable) state.
By waitable, I mean if I called wait(pid) it would return properly and not hang.
Let me also add that I am tracing a multithreaded program, therefore I cannot change the code of it. Also, I omitted this information as well but this is a Linux-based system.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你问的是进程还是线程? wait 函数作用于进程,而不是线程,因此您所写的问题无效。
对于(子)进程,您可以通过使用
WNOWAIT
标志调用waitid
来检查状态。这将使进程处于等待状态。对于线程,在某些实现上,您可以调用
pthread_kill(thread, 0)
并检查ESRCH
以确定线程是否已退出,同时离开thread< /code> 处于可加入状态。请注意,这仅当线程可连接时才有效。如果它已分离或已加入,则您将调用未定义的行为,并且您的程序应该崩溃或更糟。不幸的是,在这种情况下,不要求 pthread_kill 报告 ESRCH,因此它可能会错误地报告线程仍然存在,而实际上该线程已经终止。当然,从形式上来说,在调用 pthread_exit 和实际终止之间永远停留的线程与实际已完成终止的线程之间没有什么区别,因此问题有点无意义。换句话说,不要求可连接线程必须终止,直到 pthread_join 被阻塞等待它终止为止。
Are you asking about processes or threads? The
wait
function acts on processes, not threads, so your question as-written is not valid.For (child) processes, you can check the state by calling
waitid
with theWNOWAIT
flag. This will leave the process in a waitable state.For threads, on some implementatiosn you can call
pthread_kill(thread, 0)
and check forESRCH
to determine if the thread has exited or not, while leavingthread
in a joinable state. Note that this is valid only if the thread is joinable. If it was detached or already joined, you are invoking Undefined Behavior and your program should crash or worse. Unfortunately, there is no requirement thatpthread_kill
reportESRCH
in this case, so it might falsely report that a thread still exists when in fact it already terminated. Of course, formally there is no difference between a thread that's sitting around forever between the call topthread_exit
and actual termination, and a thread that has actually finished terminating, so the question is a bit meaningless. In other words, there's no requirement that a joinable thread ever terminate untilpthread_join
is blocked waiting for it to terminate.你想做这样的事情(伪代码)吗?
如果这确实是您想要做的,那么您就将自己暴露在竞争条件下。例如,如果
my_thread
在status(my_thread)
之后但在do_something()
之前(甚至在== waiting< /代码>)?
您可能需要考虑条件变量来在线程之间安全地传达“状态”。线程安全队列也可能是一个选择...
顺便说一句,劳伦斯利弗莫尔国家实验室在 https://computing.llnl.gov/tutorials/pthreads/(包括条件变量)。该特定文档使用 POSIX API,但解释的概念是通用的。
Do you want to do something like this (pseudo-code)?
If that is indeed what you are trying to do, you are exposing yourself to race conditions. For example, what if
my_thread
wakes up afterstatus(my_thread)
but beforedo_something()
(or even before== waiting
)?You might want to consider condition variables for safely communicating "status" between threads. Thread-safe queue might also be an option...
BTW, Lawrence Livermore National Laboratory has an excellent tutorial on multithreading concepts at https://computing.llnl.gov/tutorials/pthreads/ (including condition variables). This particular document uses POSIX API, but concepts that are explained are universal.