检查线程是一个线程提振 [英] Check if thread is a boost thread

查看:113
本文介绍了检查线程是一个线程提振的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关线程局部清理的目的,我需要创建如果当前线程是通过升压::线程创建了一个检查断言。我如何可以检查是否是这样的情况?也就是说,我怎么能检查当前线程被升压::线程来处理?

For purposes of thread local cleanup I need to create an assertion that checks if the current thread was created via boost::thread. How can I can check if this was the case? That is, how can I check if the current thread is handled by boost::thread?

我只是需要这个做线程的清除本地存储线程退出的时候。 Boost的thread_local_ptr出现如果线程本身是升压线程才能正常工作。

I simply need this to do a cleanup of thread local storage when the thread exits. Boost's thread_local_ptr appears to only work if the thread itself is a boost thread.

请注意,我不是在做一次清理检查,但有时线程的生命周期中。一些函数调用我们的API /回调(间接)造成我来分配线程本地存储中的一个。只有提高线程允许这样做,所以我需要在那一刻来检测,如果线程不升压线程。

Note that I'm not doing the check at cleanup time, but sometime during the life of the thread. Some function calls one of our API/callbacks (indirectly) causing me to allocate thread-local storage. Only boost threads are allowed to do this, so I need to detect at that moment if the thread is not a boost thread.

请参照<一个href=\"http://stackoverflow.com/questions/4592033/destruction-of-static-class-members-in-thread-local-storage\">http://stackoverflow.com/questions/4592033/destruction-of-static-class-members-in-thread-local-storage为不具有通用的清理处理的问题。我回答说,实现pthread_clenaup_push实际并不起作用:那就不叫上一个干净的退出形成螺纹

Refer to http://stackoverflow.com/questions/4592033/destruction-of-static-class-members-in-thread-local-storage for the problem of not having a generic cleanup handler. I answered that and realized pthread_clenaup_push won't actually work: it isn't called on a clean exit form the thread.

虽然我没有答案,检测升压线程所选择的答案不解决我的问题的根源。升压thread_specific_ptr的会叫他们清理任何pthread的。它必须是别的东西造成它不是为我工作,作为一个独立的测试表明,它的工作。

While I don't have answer to detect a boost thread the chosen answer does solve the root of my problem. Boost thread_specific_ptr's will call their cleanup in any pthread. It must have been something else causing it not to work for me, as an isolated test shows that it does work.

推荐答案

为您的问题premise是错误的:)的boost ::即使线程是不是升压线程thread_specific_ptr工作。想想看 - 怎样将线程特定的存储主线程工作,看到它不可能为它由升压产生的呢?我用的boost :: thread_specific_ptr从主线程很好,虽然我还没有检查的boost :: thread_specific_ptr的实现,实现它会工作即使对于非增压线程的最明显的方式。大多数操作系统让你得到一个唯一的ID号为当前线程,然后你就可以作为索引使用成图/数组/哈希表。

The premise for your question is mistaken :) boost::thread_specific_ptr works even if the thread is not a boost thread. Think about it -- how would thread specific storage for the main thread work, seeing as it's impossible for it to be created by boost? I have used boost::thread_specific_ptr from the main thread fine, and although I haven't examined boost::thread_specific_ptr's implementation, the most obvious way of implementing it would work even for non-boost threads. Most operating systems let you get a unique ID number for the current thread, which you can then use as an index into a map/array/hashtable.

你就越有可能有不同的错误,prevents你期待的发生,看的行为。你应该打开一个单独的问题用小编译code样本说明意外行为。

More likely you have a different bug that prevents the behavior you're expecting to see from happening. You should open a separate question with a small compilable code sample illustrating the unexpected behavior.

这篇关于检查线程是一个线程提振的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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