473,249 Members | 1,448 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,249 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 1336
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.