Any efficient data structure for set of `ThreadId`?


I’m wondering if there exists somewhere a data structure that could track a set of thread ids? Obviously, one could use a Mutex<HashSet<ThreadId>>, but that would make every addition or removal of a thread take the lock. Since in my scenario (which seems like it must be common) each thread would only add and remove itself, so I could imagine using lock-free thread-local storage to make this fast. I also will need to be able to (possibly slowly) identify whether the set is empty from a single thread.

Any ideas whether this sort of structure exists in one of the many threading libraries? Or something that could be used to implement this? Or how I might create such a data structure?


How often is your code going to be accessing the set? If the answer isn’t “millions of times per second”, I’d suggest using a Mutex<HashSet<ThreadId>> and profiling to see if lock contention is actually a problem.


I’m actually thinking now that I probably won’t need this data structure, but the application would be used in the deref method of a smart pointer, so it could be used billions of times per second. My current thinking is that each thread would keep track of whether it is a member of the set and then update an atomic count as needed. That gets me my needed information (which is to know when no threads are accessing the data) with hopefully minimal overhead.