开发者

C++ boost thread id and Singleton

开发者 https://www.devze.com 2022-12-27 20:39 出处:网络
Sorry to flood so many questions this week. I assume thread index returned by thread.get_id is implementation specific.

Sorry to flood so many questions this week.

I assume thread index returned by thread.get_id is implementation specific. In case of the pthreads, is index reused? IE, if thread 0 runs and joins, is thread launched afterwords going to have a different ID?

the reason I ask this is a need to implement Singleton pattern with a twist: each thread gets its own i开发者_如何学运维nstance. I know it sounds very crazy, but threads control hardware (cuda) which does not permit device memory sharing, even at thread level.

What is a good way to implement such pattern?


For a global (singleton) where each thread gets its own instance, use thread local storage. Boost has thread_specific_ptr for this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号