When designing a collection class, is there any reason not to implement locking privately to make it thread safe? Or should I leave that responsibility up to the consumer of the collection?
When designing a collection class, is there any reason not to implement locking privately
Share
It depends. Is your goal to write a collection class which is accessed by multiple threads? If so, make it thread safe. If not, don’t waste your time. This kind of thing is what people refer to when they talk about ‘premature optimization’
Solve the problems that you have. Don’t try to solve future problems that you think you may have some years in the future, because you can’t see the future, and you’ll invariably be wrong.
Note: You still need to write your code in a maintainable way, such that if you did need to come along and add locking to the collection, it wouldn’t be terribly hard. My point is ‘don’t implement features that you don’t need and won’t use’