473,406 Members | 2,698 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,406 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 1341
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
13
by: Paul | last post by:
Hi, How do I wait until a thread is finished his job then continue to the original thread? public void main(string args) { Thread t = new Thread(new ThreadStart(DoWork)); t.Start();
2
by: Sakharam Phapale | last post by:
Hi All, In following example, while playing file, if thread goes in WaitSleepJoin state, due to Thread.Sleep method. Now I want to suspend thread by clicking on cmdSuspend button. I have...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
7
by: MariusI | last post by:
Are objects implicitly stored in the TLS of the currently running thread? When creating multithreaded applications i get errors when accessing data from a different thread than the thread used to...
5
by: Alan T | last post by:
I will do several things in my thread: Copy a file to a location Update database record Read the file content Write the content to a log file If I call Thread.Abort(), it may be possible to...
5
by: Mo | last post by:
I am trying to set a text box value when data is received from the com port (barcode reader). I am getting the following error when I try to set the text box TXNumber after data is received ...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
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
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.