I have a legacy C API that I need to add functionality to. Specifically I need to make some of this API's call开发者_JS百科s thread-safe, because a call like this:
A_DoFoo(HandleA a)
Uses an object shared by all handles of type A.
My first thought is to add a function to the API that looks like this:
A_DoFooEx(HandleA a, HandleB b)
Is this a normal approach?
(I only used the Ex suffix because of years of exposure to the Win32 SDK. Does it make more sense to give a more descriptive name to the function?)
Adding another function under a new name is the standard (and in fact just about the only) way to deal with this problem in straight C (in C++ you could add a new overload and keep the old name). I'm not a big fan of the Ex
suffix, because it doesn't tell you what the difference between the old and new APIs is. On the other hand, don't saddle people with something long and heinous like A_DoFoo_ThreadSafe
, that hurts readability.
In this case, I'd try to think of a name that indicates what the requirements on HandleB
are. If you tell us what this API actually does, we can maybe make more specific suggestions.
I prefer writing wrapper for thread safe code. So I can embedded any thread un safe code and make it thread safe use wrapper.
You should always use a proper naming format. it will help you easily maintain your code and make it more readable.
精彩评论