Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to explore the contentions between the two hot topics: Clean code vs. Good performance.

(In progress ... please post comments to help me flush out the details of this question. Thanks.)

Examples which may help me define the question:

  • Windows GDI+ is known to perform work on separate threads, which would have happened in parallel to user's code if run on a multi-processor machine.
  • Component Object Model (COM) is a framework which gives class developers the choice to provide parallelizable implementations of interfaces, however bugs which arise from that is not unheard of.
  • One way of providing parallelized safety is to ensure that the parallelized work only happen within a single call to the object's method at the outermost API level; the method shouldn't return to the caller until the parallelized work is finished. (Objects and methods which aren't visible to the outside can be parallelized.) Does this make it necessary to build an entire set of flyweight objects just to separate the "Public Objects" from the "Private Objects"?
  • SOLID principles encourage extensibility by allowing library users to provide their own implementations of interfaces (through Dependency Injection). How does the library ensure that the user's implementation satisfies the thread-safety requirements?
    • Component Object Model uses "thread models" which was an attempt on this issue, however most of the time it only results in deadlocks or failed function calls for the reason that "you are calling from the wrong thread apartment".
share|improve this question
    
At least in my humble and limited experience, the problem with creating APIs encapsulating multi threaded operation is less about OOP and more about synchronous versus asynchronous code. If you have wait for a thread set to finish on every call, you might be losing a lot of the possible performance improvements. My usual solution is an "event loop" and a thread safe signal and slots mechanism, inspired by Qt. –  Vitor May 9 '11 at 0:37
    
+1 Good question. I have this doubt myself –  Edgar Gonzalez May 9 '11 at 0:48
    
@Vitor Braga: how should I design/name my interfaces/methods so that library users (fellow programmers who don't read design documents) can easily tell which part of my design operates like event loop, signal, slot, yield, or wait? –  rwong May 9 '11 at 0:56
1  
Since I came myself from a Qt background I just call events signals and callbacks slots. A connect method then links a signal to a given slot. Then I just write on doxygen "method XYZ returns immediately and raises signal ABC when finished." It has been enough for me, right now. –  Vitor May 9 '11 at 1:17
add comment

1 Answer

When you allow callers to specify a function to be executed, it is not possible to ensure that code is thread safe, at least in a general purpose language - there is nothing to stop the user code sharing variables or otherwise depending on a particular execution order. You might be able to set something up with expression trees to disallow references to anything other than local variables, but that is either way too much effort or has too many limitations.

Because of this, any special code you add for thread safety within your library is just needless complexity - specify in the docs that the functions will execute on different threads in no particular order and leave it at that.

Some careful thought around the API design may help though - if you can encourage the use of pure functions, most code will be thread safe without the user needing to think about it.

share|improve this answer
    
How do I encourage the use of pure functions? –  rwong May 9 '11 at 0:41
    
That's hard to answer for the general case, but picking the right return object is a large part of it - make sure you are never relying on side effects within your library and that the caller won't need anything that isn't returned. –  Tom Clarkson May 9 '11 at 1:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.