Check if thread is a boost thread

前端 未结 3 1858
野趣味
野趣味 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: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.

提交回复
热议问题