如何检查当前是否有任何线程正在运行

发布于 2024-09-06 11:00:30 字数 178 浏览 6 评论 0原文

我知道有一个用于多进程的

 waitpid(-1,WNOHANG,NULL)

非阻塞函数调用来检查当前是否有任何子进程正在工作

但是是否有任何类似的 lib 函数来检查多线程?

我想要做的就是检查当前是否有任何线程,如果没有则重置一些全局值。

I know there is one for multi processes

 waitpid(-1,WNOHANG,NULL)

that is non-blocking function call to check if there is any child process currently working on

But is there any similar lib function to check for multithread?

All i want to do is check if there is any thread currently on, if not reset some global values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满身野味 2024-09-13 11:00:30

这是非阻塞函数调用,用于检查当前是否有任何子进程正在处理

错误。这是一个检查是否有任何子进程尚未终止的调用。它不仅检查而且还收获终止的孩子(如果有的话)。否则,孩子们可能会处于任何可能的状态,比如陷入僵局(在我的书中,这远非有效)。

我想做的就是检查当前是否有任何线程,如果没有则重置一些全局值。

也许你应该在这里发帖作为一个问题,问你为什么要这样做。听起来你做了一些非常错误的事情。

如果您还没有为您的线程执行 pthread_join() ,则意味着您的线程已经执行 pthread_detach() 。如果添加到线程 pthread_detach() 没有问题,我认为向线程添加一些额外的代码以识别它们已经(几乎)终止(例如 sem_post())是没有问题的,以便 main() 可以注意到线程已终止(例如通过调用 sem_trylock())。

如果可移植性不是必需的,那么还可以尝试定期查询进程的操作系统线程数。

尽管在程序中存在一些具有未定义生命周期的线程,并且与主线程没有任何适当的同步,但在我看来仍然是错误的。

that is non-blocking function call to check if there is any child process currently working on

Wrong. That is a call to check if there is any child process not yet terminated. And it not only checks but also reaps a terminated child, if any. Children might be otherwise in any possible state, like hanging in a deadlock (what on my book is far from being working).

All i want to do is check if there is any thread currently on, if not reset some global values.

Probably you should post here as a question why you want to do it. It sounds that you do something terribly wrong.

If you do not do already pthread_join() for your threads, that means that your threads already do pthread_detach(). If you had no problems adding to your threads pthread_detach() I think there would be no problem to add some extra code to threads to identify that they have (almost) terminated (e.g. sem_post()) so that main() can notice that a thread had terminated (e.g. by calling sem_trylock()).

If portability isn't a requirement, then one can also try query OS number of threads of the process periodically.

Though it is still IMO wrong to have in a program some threads, with undefined life cycle, without any proper sync with main thread.

硪扪都還晓 2024-09-13 11:00:30

您可以只保存线程的句柄并使用一个函数来检查它是否仍在运行。我不确定是否有功能,但这应该可以。

You could just save the handle of a thread and have a function to check if it is still running. I'm not sure if theres a function but this should work.

捂风挽笑 2024-09-13 11:00:30

pthread_kill(pid, 0) 其中 pid 是 pthread_create 返回的线程 ID,可以告诉您线程是否仍处于活动状态。 (这就是我对你问题的理解)
如果线程仍然存在,则返回 0,否则返回错误代码。

pthread_kill(pid, 0) where pid is the thread id that pthread_create has returned can tell you if a thread is still alive. (That is how I understand your question)
It returns 0 if the thread is still alive and an error code otherwise.

趁微风不噪 2024-09-13 11:00:30

我问自己一些非常相似的问题:

POSIX用于列出进程中运行的所有 pthread 的 API 调用

在您的情况下,我将直接结束 ps -eLF

I asked myself something quite similar:

POSIX API call to list all the pthreads running in a process

In your case I would just wrapped up ps -eLF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文