
managed C++ Singleton and usage in VB
Decrypted,
Quote:
> Oh I thought you ment like ...
> __gc static class Xxx { };
> In any case, I fail to see how making the constructor static changes
> anything, multithreaed or not...
Actually, it does.
Quote:
> If you have a private constructor and a static method which calls this
> constructor if and only if it has never been called before, what does it
> matter how many threads try to capture the object? Multiple threads won't
> create multiple objects =P
If the object is a singleton, it sure matters as hell. You might very well
end up creating more than one object, which defeats the whole purpose of
making it a singleton.
You're confusing what a static constructor does, though. The runtime
guarantees that the static constructor will be called exactly once, even in
the presence of multiple threads, the first time a static member of the type
is accessed.
Quote:
> To take it furthure...
> (if instance == 0) instance = new ClassName();
> Is it even possible for a thread to get throgh the if and stop right
before
> class instantiation only to let another thread start up and complete the
if
> check and actualy instatiate the object then stopping and the first thread
> instatiates again???
Yep. Very possible.
Quote:
> I always thought threaed or not, an executable line is
> locked.
Not at all. A line of code could generate dozens of individual instructions.
And why lock them, anyway? most apps aren't multithreaded, and so, locking
on them would only cause a performance hit.
Quote:
> Rather, if a thread starts to execute a line of code it does not
> stop until it hits a semi-collon....am I wrong on this?
Completely :)
--
Tomas Restrepo