Take the 2-minute tour ×

OpenThreads states on its website:

It is of importance to note that while a factory pattern design could have 
been used to achieve the goal of genaric interface, it would have required 
the programmer to allocate each of the 4 fundemental types 
(Thread, Mutex, Barrier, & Condition ) on the heap.  Due 
to the cost associated with heap allocation of the underlying 
concrete implementations of these constructs on some platforms, 
such allocation was deemed unacceptable at the time this library 
was originally written, and thus the factory pattern was not used. 

Instead, a somewhat obtuse - but effective - technique was 
chosen to provide the necessary data/implementation hiding. 
This technique uses private void pointers to encapsulate object private data.  
The void pointers actually point at concrete data structures, 
but give a uniform interface to the dso.

Looking at the implementation, though, all those private void pointers seem allocated by new. For instance, in the PThreads barrier constructor, the following code is called:

PThreadBarrierPrivateData *pd = new PThreadBarrierPrivateData();

How does that avoid heap allocation?

Furthermore, would simply specifying an interface in a header file and providing multiple implementations (one of which would selected at compile time) be just as effective in providing data hiding?

I suppose that I am missing something/ not seeing the whole picture, so any enlightenment would be appreciated.


share|improve this question
It says "...at the time it was originally written...". I could imagine that the code was different in previous version. Now there is no more concern about the "heap"-issue, but to stay compatible with (the interface to) earlier versions of the library, they still haven't changed to a factory pattern. –  AudioDroid Jul 20 '11 at 13:28
That is a possibility; however my point was that for compile-time choice, a factory is not even needed. The factory pattern is mostly used for run-time configurability. –  Rom1 Jul 20 '11 at 15:45
I think they don't care that much about where the memory is allocated but how you use it. I bet you can write Thread t; and be just fine. This is much more convenient to use than using new all the time. My 2 cts only. –  J.N. Jul 21 '11 at 7:42
add comment


Your Answer



Browse other questions tagged or ask your own question.