On Jun 20, 9:38 pm, kalman <mend...@gmail.com> wrote:
Hi all,
I've recently stepped in a project of medium size, and at the easy
question: "are those class instances shared between two or more
threads" the response was: "no... wait... yes, well I'm not sure... I
don't know...". Riiiight.
I used the following technique that should permit to detect (at
runtime, sigh!) if two or more threads are using concurrently a class.
I think the approach is faulty. If you want to know if an instance is
used by more than one thread, you shouln't rely on it being actually
used concurrently in one particular execution - threading being what
it is, you might miss the shared usage.
Instead, remember the current thread ID in the constructor and never
reset it. Test at every function entry. This way you'll definitely see
if more than one thread accesses an instance during its lifetime, even
if the accesses happen to not occur at exactly the same time.
(in a typical implementation). If some global objects are only ever
used by another thread this approach may not work.
[ comp.lang.c++.moderated. First time posters: Do this! ]