473,394 Members | 2,031 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,394 software developers and data experts.

scope, lifetime, changing mindset from one user to many simultaneous users

With a non-server app there is one instance of the program running and one
user 'using' it at a time. With this scenario I'm pretty comfortable with
variable scope and lifetime. With a server app there is one instance of the
program running but several simultaneous clients connecting to and 'using'
it. When I think about this I'm wondering what this may add to what needs to
be considered for scope and lifetime... is a scenario created where one
client will mess up another client if not handled right? I'm not referring
to thread synchronization. I'm comfortable with knowing when to lock, for
the most part. See very short partial code example and the 2 statements
following it and you will see what I'm talking about (as well as my lack of
experience :) )
class Server
{
//class globals
string g_s;
int g_i;

void methodA()
{
//member vars
int i;
string s;
}

int main(string[] args)
{
//code
}
}

assumption 1: every client would actually be accessing the same g_s and g_i
variables... eg. client1 sets g_i to 5, then client2 sets it to 2, then when
client1 reads it again it will be 2, not 5. (true/false)?

assumption 2: every client would be using their own distinct instances of i
and s when methodA() runs.... eg. client1 sets i to 5, client2 also running
sets i to 2, then when client1 reads it again it will still be 5.
(true/false)?

and finally: how would above two assumptions change, if at all, if methodA()
were declared static? or the variables were declared static?
Jun 12 '07 #1
9 1965
David,

With a server app, you don't necessarily have to have multiple users
connecting to it. The server could simply respond to changes in the system,
in that sense, there is only ever one user, the user that the service is
running under.

As far as your code example is concerned, I think that you can't really
use that as an example. The reason for this is that requests from a client
will be triggered by some sort of an event, and usually, those events will
execute on distinct threads. Now, from that point on, you have to worry
about shared variables, most notably, static variables, or classes where you
share the instance between threads.

I would recommend you look at the lock keyword, as you will be using
this a good deal when working with instances/values that are shared among
multiple threads.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <no****@nospam.comwrote in message
news:et**************@TK2MSFTNGP03.phx.gbl...
With a non-server app there is one instance of the program running and one
user 'using' it at a time. With this scenario I'm pretty comfortable with
variable scope and lifetime. With a server app there is one instance of
the program running but several simultaneous clients connecting to and
'using' it. When I think about this I'm wondering what this may add to
what needs to be considered for scope and lifetime... is a scenario
created where one client will mess up another client if not handled right?
I'm not referring to thread synchronization. I'm comfortable with knowing
when to lock, for the most part. See very short partial code example and
the 2 statements following it and you will see what I'm talking about (as
well as my lack of experience :) )
class Server
{
//class globals
string g_s;
int g_i;

void methodA()
{
//member vars
int i;
string s;
}

int main(string[] args)
{
//code
}
}

assumption 1: every client would actually be accessing the same g_s and
g_i variables... eg. client1 sets g_i to 5, then client2 sets it to 2,
then when client1 reads it again it will be 2, not 5. (true/false)?

assumption 2: every client would be using their own distinct instances of
i and s when methodA() runs.... eg. client1 sets i to 5, client2 also
running sets i to 2, then when client1 reads it again it will still be 5.
(true/false)?

and finally: how would above two assumptions change, if at all, if
methodA() were declared static? or the variables were declared static?
Jun 12 '07 #2

"David" <no****@nospam.comha scritto nel messaggio
news:et**************@TK2MSFTNGP03.phx.gbl...
With a non-server app there is one instance of the program running and one
user 'using' it at a time. With this scenario I'm pretty comfortable with
variable scope and lifetime. With a server app there is one instance of
the program running but several simultaneous clients connecting to and
'using' it. When I think about this I'm wondering what this may add to
what needs to be considered for scope and lifetime... is a scenario
created where one client will mess up another client if not handled right?
I'm not referring to thread synchronization. I'm comfortable with knowing
when to lock, for the most part. See very short partial code example and
the 2 statements following it and you will see what I'm talking about (as
well as my lack of experience :) )
What scenario you have in mind when you say "server app"?
For example, for server app I can think something simple as class library to
ASP.NET application, WebService.. many things.

If you are concerned about multiple access to same things (change the word
users of the title of your post to threads), maybe these can get you
startring:

http://www.c-sharpcorner.com/UploadF...d-0b39ad885705
http://www.codersource.net/csharp_tu...threading.html
http://msdn.microsoft.com/msdnmag/is...8/Concurrency/

>
class Server
{
//class globals
string g_s;
int g_i;

void methodA()
{
//member vars
int i;
string s;
}

int main(string[] args)
{
//code
}
}

assumption 1: every client would actually be accessing the same g_s and
g_i variables... eg. client1 sets g_i to 5, then client2 sets it to 2,
then when client1 reads it again it will be 2, not 5. (true/false)?
True, IFF the clients are using the same object on the server (aka,
singleton object, class instance).
False, if every client creates it's own object on the server.
assumption 2: every client would be using their own distinct instances of
i and s when methodA() runs.... eg. client1 sets i to 5, client2 also
running sets i to 2, then when client1 reads it again it will still be 5.
(true/false)?
True.
and finally: how would above two assumptions change, if at all, if
methodA() were declared static? or the variables were declared static?
Any variable declared static is global to every user of the class.
Changing methodA() to static would not change much in this case (static
method cannot access any variables that are not static, so in your case,
static methodA() would not "see" g_s, for example.
Jun 12 '07 #3
thanks for the reply Nicholas. Comments inline.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:49**********************************@microsof t.com...
David,

With a server app, you don't necessarily have to have multiple users
connecting to it. The server could simply respond to changes in the
system, in that sense, there is only ever one user, the user that the
service is running under.
true, my question wouldn't apply to that scenario.
>
As far as your code example is concerned, I think that you can't really
use that as an example. The reason for this is that requests from a
client will be triggered by some sort of an event, and usually, those
events will execute on distinct threads. Now, from that point on, you
have to worry about shared variables, most notably, static variables, or
classes where you share the instance between threads.

I would recommend you look at the lock keyword, as you will be using
this a good deal when working with instances/values that are shared among
multiple threads.
I know that if a variable is accessible by multiple threads I need to
synchronize access to it.. but thats in cases where I expect shared access.
What I'm getting at (but not very concisely) is keeping clients separate.
So, based on my current understanding, the class global variables in my
example below would need locking, as they exist once per instance of this
class (which is the program), and could be accessed by many different
threads (clients). This is a place where you could put variables you intend
for all clients to use... in other words you expect client1 to change it and
for that change to be visible to client2. What I am more unsure of is the
case you do want client1's variables to only be client1's. In this case
there should be no locking required. So I gave this simple class example
just to show placement of the variables, class global, or declared in the
member method, because I have been working under the assumption that the
member method variables would be distinct (newly created each time method
runs for each client) per client.

to (hopefully) clarify my point of uncertianty: in a one user app, each time
member method runs, variables declared within it are created, they are
accessible only to that member method, and live until the method ends, when
the method is called again, new vars are created. And again, its a one user
app so the member method could never be executing more than once
simultaneously. The *new* factor takes that and adds the fact that there are
now many users, so now the member method *could* be running several times
simultaneously. That said, I was really wondering if each instance of the
method executing would maintain distinct variables... thinking of scenarios
where that *is* what you need, not scenarios where shared access is desired.

If my 2 assumptions are correct, then great. I think they are, especially
the first, less sure of the second, but very unsure of how 'static' may
change those assumptions. Hopefully that clarifies my post. I apologize, I
know I really suck at formulating these questions... I don't speak the speak
well enough. : (
>

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David" <no****@nospam.comwrote in message
news:et**************@TK2MSFTNGP03.phx.gbl...
>With a non-server app there is one instance of the program running and
one user 'using' it at a time. With this scenario I'm pretty comfortable
with variable scope and lifetime. With a server app there is one instance
of the program running but several simultaneous clients connecting to and
'using' it. When I think about this I'm wondering what this may add to
what needs to be considered for scope and lifetime... is a scenario
created where one client will mess up another client if not handled
right? I'm not referring to thread synchronization. I'm comfortable with
knowing when to lock, for the most part. See very short partial code
example and the 2 statements following it and you will see what I'm
talking about (as well as my lack of experience :) )
class Server
{
//class globals
string g_s;
int g_i;

void methodA()
{
//member vars
int i;
string s;
}

int main(string[] args)
{
//code
}
}

assumption 1: every client would actually be accessing the same g_s and
g_i variables... eg. client1 sets g_i to 5, then client2 sets it to 2,
then when client1 reads it again it will be 2, not 5. (true/false)?

assumption 2: every client would be using their own distinct instances of
i and s when methodA() runs.... eg. client1 sets i to 5, client2 also
running sets i to 2, then when client1 reads it again it will still be 5.
(true/false)?

and finally: how would above two assumptions change, if at all, if
methodA() were declared static? or the variables were declared static?

Jun 12 '07 #4
thanks Laura. I didn't think the server type mattered for what I was getting
at, I just said 'server' as it is indicative of multiple simultaneous users.
It was not concerned with thread synch, more so the opposite of 'shared'
access. Your responses to my assumptions told me exactly what I needed to
know. Thanks.
"Laura T." <LT*****@yahoo.comwrote in message
news:OH**************@TK2MSFTNGP02.phx.gbl...
>
"David" <no****@nospam.comha scritto nel messaggio
news:et**************@TK2MSFTNGP03.phx.gbl...
>With a non-server app there is one instance of the program running and
one user 'using' it at a time. With this scenario I'm pretty comfortable
with variable scope and lifetime. With a server app there is one instance
of the program running but several simultaneous clients connecting to and
'using' it. When I think about this I'm wondering what this may add to
what needs to be considered for scope and lifetime... is a scenario
created where one client will mess up another client if not handled
right? I'm not referring to thread synchronization. I'm comfortable with
knowing when to lock, for the most part. See very short partial code
example and the 2 statements following it and you will see what I'm
talking about (as well as my lack of experience :) )
What scenario you have in mind when you say "server app"?
For example, for server app I can think something simple as class library
to ASP.NET application, WebService.. many things.

If you are concerned about multiple access to same things (change the word
users of the title of your post to threads), maybe these can get you
startring:

http://www.c-sharpcorner.com/UploadF...d-0b39ad885705
http://www.codersource.net/csharp_tu...threading.html
http://msdn.microsoft.com/msdnmag/is...8/Concurrency/

>>
class Server
{
//class globals
string g_s;
int g_i;

void methodA()
{
//member vars
int i;
string s;
}

int main(string[] args)
{
//code
}
}

assumption 1: every client would actually be accessing the same g_s and
g_i variables... eg. client1 sets g_i to 5, then client2 sets it to 2,
then when client1 reads it again it will be 2, not 5. (true/false)?
True, IFF the clients are using the same object on the server (aka,
singleton object, class instance).
False, if every client creates it's own object on the server.
>assumption 2: every client would be using their own distinct instances of
i and s when methodA() runs.... eg. client1 sets i to 5, client2 also
running sets i to 2, then when client1 reads it again it will still be 5.
(true/false)?
True.
>and finally: how would above two assumptions change, if at all, if
methodA() were declared static? or the variables were declared static?
Any variable declared static is global to every user of the class.
Changing methodA() to static would not change much in this case (static
method cannot access any variables that are not static, so in your case,
static methodA() would not "see" g_s, for example.


Jun 12 '07 #5
.... follow up question: You said: Any variable declared static is global to
every user of the class.

does this mean a static var is persistent between multiple _instances_ of a
class?

class example
{
static string s;
static int i;

void methodA()
{
s = "ladada"
i = 8;
}
}

example myExample = new example();
example myExample2 = new example();

myExample.methodA();
console.writeline("{0}", myExample2.s);

would write ladada to the console?

"Laura T." <LT*****@yahoo.comwrote in message
news:OH**************@TK2MSFTNGP02.phx.gbl...
>
"David" <no****@nospam.comha scritto nel messaggio
news:et**************@TK2MSFTNGP03.phx.gbl...
>With a non-server app there is one instance of the program running and
one user 'using' it at a time. With this scenario I'm pretty comfortable
with variable scope and lifetime. With a server app there is one instance
of the program running but several simultaneous clients connecting to and
'using' it. When I think about this I'm wondering what this may add to
what needs to be considered for scope and lifetime... is a scenario
created where one client will mess up another client if not handled
right? I'm not referring to thread synchronization. I'm comfortable with
knowing when to lock, for the most part. See very short partial code
example and the 2 statements following it and you will see what I'm
talking about (as well as my lack of experience :) )
What scenario you have in mind when you say "server app"?
For example, for server app I can think something simple as class library
to ASP.NET application, WebService.. many things.

If you are concerned about multiple access to same things (change the word
users of the title of your post to threads), maybe these can get you
startring:

http://www.c-sharpcorner.com/UploadF...d-0b39ad885705
http://www.codersource.net/csharp_tu...threading.html
http://msdn.microsoft.com/msdnmag/is...8/Concurrency/

>>
class Server
{
//class globals
string g_s;
int g_i;

void methodA()
{
//member vars
int i;
string s;
}

int main(string[] args)
{
//code
}
}

assumption 1: every client would actually be accessing the same g_s and
g_i variables... eg. client1 sets g_i to 5, then client2 sets it to 2,
then when client1 reads it again it will be 2, not 5. (true/false)?
True, IFF the clients are using the same object on the server (aka,
singleton object, class instance).
False, if every client creates it's own object on the server.
>assumption 2: every client would be using their own distinct instances of
i and s when methodA() runs.... eg. client1 sets i to 5, client2 also
running sets i to 2, then when client1 reads it again it will still be 5.
(true/false)?
True.
>and finally: how would above two assumptions change, if at all, if
methodA() were declared static? or the variables were declared static?
Any variable declared static is global to every user of the class.
Changing methodA() to static would not change much in this case (static
method cannot access any variables that are not static, so in your case,
static methodA() would not "see" g_s, for example.


Jun 12 '07 #6
On Tue, 12 Jun 2007 09:02:47 -0700, David <no****@nospam.comwrote:
... follow up question: You said: Any variable declared static is global
to
every user of the class.

does this mean a static var is persistent between multiple _instances_
of a
class?
Yes.

I was going to answer your original post, but it looks like you've gotten
some pretty good advice already. :)

Pete
Jun 12 '07 #7
David <no****@nospam.comwrote:
... follow up question: You said: Any variable declared static is global to
every user of the class.

does this mean a static var is persistent between multiple _instances_ of a
class?
Sort of - a static variable isn't associated with *any* instance of the
class - so there could be no instances, or there could be a million,
and there'll only be one variable.
class example
{
static string s;
static int i;

void methodA()
{
s = "ladada"
i = 8;
}
}

example myExample = new example();
example myExample2 = new example();

myExample.methodA();
console.writeline("{0}", myExample2.s);

would write ladada to the console?
No - it wouldn't compile because you can't access static variables
through instance expressions. If you print example.s then that would
indeed print ladada though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 12 '07 #8
thanks Pete.

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 12 Jun 2007 09:02:47 -0700, David <no****@nospam.comwrote:
>... follow up question: You said: Any variable declared static is global
to
every user of the class.

does this mean a static var is persistent between multiple _instances_
of a
class?

Yes.

I was going to answer your original post, but it looks like you've gotten
some pretty good advice already. :)

Pete

Jun 12 '07 #9
thanks Jon.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
David <no****@nospam.comwrote:
>... follow up question: You said: Any variable declared static is global
to
every user of the class.

does this mean a static var is persistent between multiple _instances_
of a
class?

Sort of - a static variable isn't associated with *any* instance of the
class - so there could be no instances, or there could be a million,
and there'll only be one variable.
>class example
{
static string s;
static int i;

void methodA()
{
s = "ladada"
i = 8;
}
}

example myExample = new example();
example myExample2 = new example();

myExample.methodA();
console.writeline("{0}", myExample2.s);

would write ladada to the console?

No - it wouldn't compile because you can't access static variables
through instance expressions. If you print example.s then that would
indeed print ladada though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 12 '07 #10

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

Similar topics

3
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called...
18
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
3
by: Michael Roper | last post by:
Is there a C# approach that allows you to define locals with program lifetime? That is, I don't want to have to expand the scope of a variable just so I can make it persistent. For example,...
23
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
10
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers...
6
by: Neelesh Bodas | last post by:
Hello All, I was just listing down various ways in which variables can be created and destroyed in C++. (On the lines of 10.4.3 TC++PL Ed 3) Putting the summary here for corrections, comments,...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.