471,350 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

thread calling methods

When a thread calls a method (and/or that method calls another method), are they thread safe if the first method is controlled on access?
Jan 5 '06 #1
6 1276
Pohihihi,

If a method calls another method and only one thread can execute the first method at a time I'd say both are thread safe because only one thread executes the methods. However this is very simplistic a lot of factors get involved in a real application and it also depends on what is your definition of thread safety.
--

Stoitcho Goutsev (100)
"Pohihihi" <no*****@hotmail.com> wrote in message news:%2***************@tk2msftngp13.phx.gbl...
When a thread calls a method (and/or that method calls another method), are they thread safe if the first method is controlled on access?
Jan 5 '06 #2
Pohihihi <no*****@hotmail.com> wrote:
When a thread calls a method (and/or that method calls another
method), are they thread safe if the first method is controlled on
access?


What do you mean, exactly? Suppose FirstMethod() calls SecondMethod().
Even if there are locks to make sure that FirstMethod() is only ever
executing in one thread at a time, another thread might call
SecondMethod() directly (without going through FirstMethod()) so you
could still have two threads executing SecondMethod() at the same time.

Does that answer your question?

--
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
Jan 5 '06 #3
Yes that is what I was looking for. Thanks.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pohihihi <no*****@hotmail.com> wrote:
When a thread calls a method (and/or that method calls another
method), are they thread safe if the first method is controlled on
access?


What do you mean, exactly? Suppose FirstMethod() calls SecondMethod().
Even if there are locks to make sure that FirstMethod() is only ever
executing in one thread at a time, another thread might call
SecondMethod() directly (without going through FirstMethod()) so you
could still have two threads executing SecondMethod() at the same time.

Does that answer your question?

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

Jan 5 '06 #4
Late thought, how can I make all the methods called in a thread as thread
safe?
Putting lock while calling might not be a very good option as first thread
might wait for some other thread to process. In other words how can I manage
to get all the methods in one thread without busy waiting in case locking
threads. Any other way to do that?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pohihihi <no*****@hotmail.com> wrote:
When a thread calls a method (and/or that method calls another
method), are they thread safe if the first method is controlled on
access?


What do you mean, exactly? Suppose FirstMethod() calls SecondMethod().
Even if there are locks to make sure that FirstMethod() is only ever
executing in one thread at a time, another thread might call
SecondMethod() directly (without going through FirstMethod()) so you
could still have two threads executing SecondMethod() at the same time.

Does that answer your question?

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

Jan 5 '06 #5
Pohihihi <no*****@hotmail.com> wrote:
Late thought, how can I make all the methods called in a thread as thread
safe?
Putting lock while calling might not be a very good option as first thread
might wait for some other thread to process. In other words how can I manage
to get all the methods in one thread without busy waiting in case locking
threads. Any other way to do that?


It's very hard to give an answer without a more concrete example of
what you're after. One nice ideal to strive for is to make threads as
separate as possible, so they only need to lock for very brief periods.
Most types don't really need to be threadsafe - but their safety needs
to be documented.

--
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
Jan 5 '06 #6
thanks actually that gives me nice start.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pohihihi <no*****@hotmail.com> wrote:
Late thought, how can I make all the methods called in a thread as thread
safe?
Putting lock while calling might not be a very good option as first
thread
might wait for some other thread to process. In other words how can I
manage
to get all the methods in one thread without busy waiting in case locking
threads. Any other way to do that?


It's very hard to give an answer without a more concrete example of
what you're after. One nice ideal to strive for is to make threads as
separate as possible, so they only need to lock for very brief periods.
Most types don't really need to be threadsafe - but their safety needs
to be documented.

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

Jan 6 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Steven Brown | last post: by
20 posts views Thread by Doug Thews | last post: by
13 posts views Thread by Paul | last post: by
7 posts views Thread by Charles Law | last post: by
5 posts views Thread by Alan T | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.