473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Singleton

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
--
Be Cool!
Nov 22 '05 #1
3 1205
thejackofall <th**********@discussions.microsoft.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
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

--
Be Cool!
"Jon Skeet [C# MVP]" wrote:
thejackofall <th**********@discussions.microsoft.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 22 '05 #3
thejackofall <th**********@discussions.microsoft.com> wrote:
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..

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
6
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.