Singleton

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi.

I would like to know how a singleton object works. If you have a singleton
object, you have a single object ( brilliant! ). Within a process, if you
call X.Login() two or more times, and you don't have a shared resource within
the method called Login(), it's not necessary to synchronize the method call.

Suppose you have foo() as

int Login()
{
DataSet oDS = new DataSet();
oDS.DoSomething();
}

So, the question is how does it work when you have a singleton object, and
you call its method more than once at the same time?

If X.Login() is called twice immediately after another within a web
application, and X is a singleton, would there be a problem? Why?

Thanks.
J
 
thejackofall said:
I would like to know how a singleton object works. If you have a singleton
object, you have a single object ( brilliant! ). Within a process, if you
call X.Login() two or more times, and you don't have a shared resource within
the method called Login(), it's not necessary to synchronize the method call.

Suppose you have foo() as

int Login()
{
DataSet oDS = new DataSet();
oDS.DoSomething();
}

So, the question is how does it work when you have a singleton object, and
you call its method more than once at the same time?

If X.Login() is called twice immediately after another within a web
application, and X is a singleton, would there be a problem? Why?

No, there wouldn't be a problem, even if two different threads are both
calling it at the *same* time.

You'd only run into problems if they both needed to share the same
data.
 
Jon,

Thanks for your email. While I am willing to accept your answer, I would
like to have a little more information. So, here we go.

When you call a method, basically, it's pointing to the address of code that
need to be executed. If X.Login() is called multiple times at the same time,
and therefore, DataSet oDS = new DataSet() is called multiple times. So,
wouldn't there be a conflict in the memory if there is only one set of code
that's executing unless it maintains a separate copy of code or stack for
example.

Thanks.
J
 
thejackofall said:
Thanks for your email. While I am willing to accept your answer, I would
like to have a little more information. So, here we go.

When you call a method, basically, it's pointing to the address of code that
need to be executed. If X.Login() is called multiple times at the same time,
and therefore, DataSet oDS = new DataSet() is called multiple times. So,
wouldn't there be a conflict in the memory if there is only one set of code
that's executing unless it maintains a separate copy of code or stack for
example.

And that's exactly what it does - if the calls are in different
threads, each thread has a different stack. If it's within the same
stack, then it's in a different stack frame..
 
Back
Top