如何查询pthread以查看它是否仍在运行? [英] How do you query a pthread to see if it is still running?

查看:2163
本文介绍了如何查询pthread以查看它是否仍在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的析构函数中,我想要清除一个线程。



我的目标是等待一个线程完成执行,然后销毁线程。

我发现有关查询pthread的状态的唯一方法是 pthread_attr_setdetachstate ,但这只会告诉您您的主题是否为:




  • PTHREAD_CREATE_DETACHED

  • PTHREAD_CREATE_JOINABLE



这些都与线程是否仍在运行无关。



如何查询pthread以查看它是否仍在运行?

解决方案

这里似乎有两个问题:



如何等到我的线程完成?



答案:这是由pthreads直接支持的 - 使你的线程被停止的JOINABLE(当它第一次启动),并使用pthread_join()阻塞当前线程,直到线程待-stopped不再运行。






如何判断我的线程是否仍在运行?



答案:你可以添加一个thread_complete标志来执行:



场景:线程A想知道线程B是否



当线程B被创建时,它被赋予一个指向thread_complete标志地址的指针。在创建线程之前,thread_complete标志应初始化为NOT_COMPLETED。线程B的入口点函数应该立即调用pthread_cleanup_push()推送一个清理处理程序,将thread_complete标志设置为COMPLETED。



有关清理处理程序的详细信息, pthread清理处理程序



您将需要包括相应的pthread_cleanup_pop(1)调用,以确保清除处理程序被调用,无论什么(即如果线程退出正常或由于取消等)。



然后,线程A可以简单地检查thread_complete标志,看看线程B是否已经退出。



注意:你的thread_complete 标志应该被声明为volatile,并且应该是一个原子类型 - GNU编译器为此目的提供sig_atomic_t。这允许两个线程一致地访问相同的数据,而不需要同步结构(互斥体/信号量)。


In my destructor I want to destroy a thread cleanly.

My goal is to wait for a thread to finish executing and THEN destroy the thread.

The only thing I found about querying the state of a pthread is pthread_attr_setdetachstate but this only tells you if your thread is:

  • PTHREAD_CREATE_DETACHED
  • PTHREAD_CREATE_JOINABLE

Both of those have nothing to do with whether the thread is still running or not.

How do you query a pthread to see if it is still running?

解决方案

It sounds like you have two questions here:

How can I wait until my thread completes?

Answer: This is directly supported by pthreads -- make your thread-to-be-stopped JOINABLE (when it is first started), and use pthread_join() to block your current thread until the thread-to-be-stopped is no longer running.


How can I tell if my thread is still running?

Answer: You can add a "thread_complete" flag to do the trick:

Scenario: Thread A wants to know if Thread B is still alive.

When Thread B is created, it is given a pointer to the "thread_complete" flag address. The "thread_complete" flag should be initialized to NOT_COMPLETED before the thread is created. Thread B's entry point function should immediately call pthread_cleanup_push() to push a "cleanup handler" which sets the "thread_complete" flag to COMPLETED.

See details about cleanup handlers here: pthread cleanup handlers

You'll want to include a corresponding pthread_cleanup_pop(1) call to ensure that the cleanup handler gets called no matter what (i.e. if the thread exits normally OR due to cancellation, etc.).

Then, Thread A can simply check the "thread_complete" flag to see if Thread B has exited yet.

NOTE: Your "thread_complete" flag should be declared "volatile" and should be an atomic type -- the GNU compilers provide the sig_atomic_t for this purpose. This allows the two threads consistent access the same data without the need for synchronization constructs (mutexes/semaphores).

这篇关于如何查询pthread以查看它是否仍在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆