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

D

David

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?
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
L

Laura T.

David said:
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/Upload...rticleID=920ecafc-e83b-4a9c-a64d-0b39ad885705
http://www.codersource.net/csharp_tutorial_multithreading.html
http://msdn.microsoft.com/msdnmag/issues/05/08/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.
 
D

David

thanks for the reply Nicholas. Comments inline.

Nicholas Paldino said:
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]
- (e-mail address removed)

David said:
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?
 
D

David

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. said:
David said:
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/Upload...rticleID=920ecafc-e83b-4a9c-a64d-0b39ad885705
http://www.codersource.net/csharp_tutorial_multithreading.html
http://msdn.microsoft.com/msdnmag/issues/05/08/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.
 
D

David

.... 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. said:
David said:
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/Upload...rticleID=920ecafc-e83b-4a9c-a64d-0b39ad885705
http://www.codersource.net/csharp_tutorial_multithreading.html
http://msdn.microsoft.com/msdnmag/issues/05/08/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.
 
P

Peter Duniho

... 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
 
J

Jon Skeet [C# MVP]

David said:
... 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.
 
D

David

thanks Jon.

Jon Skeet said:
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.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top