Re: Suspected Memory Leak in Multithread queue implmenetation
2. Would you guys mind critique if I've done anything wrong on my
stl::queue multithread implemenation?
Thank you very much. I am really appreciate yours help,
I cannot help you debug your code unfortunately, so pardon my critique.
1. You should maintain orthogonality of your primitives. Instead of
making a "thread safe std::queue<>", you should make a template class
that provides fast-mutex (critical section in Windows) that can be
applied to any (aggregate) type. Certainly you would not want to
rewrite protection code for every standard data structure.
2. Related to #1, you should not put protection inside the data
structure itself. For example, in your code, you have a member
function that returns the size of the queue. You do this by acquiring
critical section, grabbing the size, releasing critical section, and
returning the copy of the size. This is bad because the copy you
receive will become essentially useless after it is returned by the
function. The global variable will be free for access by another
thread, and it is entirely conceivable that the size could drop by,
say, 10, while you are looking at the size you just got. In a
nutshell, if you ever need to do something to a global variable in a
multi-threaded application, acquire the variable, do what you have to
do, then let it go. Once you let it go, you must assume that its state
is unknown until the next acquisition.
Here is actual code that I use for a shared queue of the kind you are
struct Arrivals : Shared<Counted<Queue<Network_Interface::Frame *> >
Arrivals (const Arrivals &);
Arrivals & operator = (const Arrivals &);
The above code, in English reads, "struct Arrivals is a shared, counted
queue of pointers to network interface frames..."
Here, I get protection from a Shared<> template, and I get semaphoric
counting using the Counted<> template, so a Shared<Counted<Whatever> >
can be acquired, have its count raised by N (N = 1 by default), and
Another thread can block until count is at least 1, then it would do
It should be possible to make these interfaces portable (Windows/Unix).
-Le Chaud Lapin-
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]