Check if thread is a boost thread

前端 未结 3 1827
野趣味
野趣味 2021-01-19 06:55

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

相关标签:
3条回答
  • 2021-01-19 07:32

    Each time a boost thread ends, all the Thread Specific Data gets cleaned. TSD is a pointer, calling delete p* at destruction/reset.

    Optionally, instead of delete p*, a cleanup handler can get called for each item. That handler is specified on the TLS constructor, and you can use the cleanup function to do the one time cleaning.

    #include <iostream>
    #include <boost/thread/thread.hpp>
    #include <boost/thread/tss.hpp>
    
    void cleanup(int* _ignored) {
        std::cout << "TLS cleanup" << std::endl;
    }
    
    void thread_func() {
        boost::thread_specific_ptr<int> x(cleanup);
        x.reset((int*)1); // Force cleanup to be called on this thread
    
        std::cout << "Thread begin" << std::endl;
    }
    
    int main(int argc, char** argv) {
        boost::thread::thread t(thread_func);
    
        t.join();
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-19 07:45

    You can't do this with a static assertion: That would mean you could detect it at compile time, and that's impossible.

    Assuming you mean a runtime check though:

    If you don't mix boost::thread with other methods, then the problem just goes away. Any libraries that are creating threads should already be dealing with their own threads automatically (or per a shutdown function the API documents that you must call).

    Otherwise you can keep, for example, a container of all pthread_ts you create not using boost::thread and check if the thread is in the container when shutting down. If it's not in the container then it was created using boost::thread.

    EDIT: Instead of trying to detect if it was created with boost::thread, have you considered setting up your application so that the API callback can only occur in threads created with boost::thread? This way you prevent the problem up front and eliminate the need for a check that, if it even exists, would be painful to implement.

    0 讨论(0)
  • 2021-01-19 07:53

    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.

    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.

    0 讨论(0)
提交回复
热议问题