Friday, July 2, 2010
Thread Safety
I've been thinking about the idea of thread safe data structures lately, and I've been a little torn. On one hand, I would think that all data structures should be thread safe in a reader/writer fashion, but on the other hand, some people are bound to complain that they don't want to lose clock cycles over synchronization when their program is only a single thread. That isn't really all that big of a deal to me (now anyway), but the thing that is annoying to me is that if all data structures are thread safe, then according to my principles of ease of use, all data structures should document that they are thread safe. I guess that's really not an issue though, because whether they are thread safe or not will have to be documented. I guess the real problem is that people should have options. They should have a choice. If everything is thread safe by default, then they have no choice on the matter. So, my thoughts have been that I would create an overloaded function called ThreadSafe(). This function would take in a bunch of different overloaded parameters, but the basic functionality would be that you pass a data structure to the ThreadSafe function, and then it would wrap and IThreadSafe decorator to the class. Of course I'm sure the naming will change, but this would significantly reduce code in large inheritance hierarchies. After the decorator is applied, all access to the inner data structure would go through the decorator, allowing for synchronization to occur. It wouldn't be hard for the user of such a library to create their own overloaded ThreadSafe() functions, where they can pass in their own data structures and follow the same pattern, keeping synchronization consistent throughout the program. It isn't hard to imagine ThreadSafe number objects, strings, and other types. The user wouldn't need to state that the variable 'x' is a ThreadSafe object; the function wrapper says it for you. Also, if you did it this way, you would need one ThreadSafe class per interface, whereas if you sub-classed class 'Y' to be 'ThreadSafeY', then there would need to be one 'ThreadSafeY' type class for every concrete class that wanted to be thread safe.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment