469,898 Members | 2,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Simple Threading Question

I have a small program that executes a single very long running
background thread today.

The scope of all variables in the method that is running in this
thread today are all local to the method. The method doesn't do
anything outside of its local scope.

My question... can I start up a 2nd (3rd, and 4th and so on) thread
running the same method? Is that safe?

Todays code (abridged edition):

Thread t1 = new Thread(new ThreadStart(DoWork));
t1.Start();

public void DoWork()
{
//... do lots of work here...
}
I would like to change the code to...

Thread t1 = new Thread(new ThreadStart(DoWork));
t1.Start();

Thread t2 = new Thread(new ThreadStart(DoWork));
t2.Start();

public void DoWork()
{
//... do lots of work here...
}
Looking at this I know that it is VERY simple and I would suspect that
it would work but why? Can you explain why this works, if it does?
Mar 19 '06 #1
3 1155
Ryan Pedersen <pe**********@gmail.com> wrote:
I have a small program that executes a single very long running
background thread today.

The scope of all variables in the method that is running in this
thread today are all local to the method. The method doesn't do
anything outside of its local scope.

My question... can I start up a 2nd (3rd, and 4th and so on) thread
running the same method? Is that safe?
Yup, that's absolutely fine.
Looking at this I know that it is VERY simple and I would suspect that
it would work but why? Can you explain why this works, if it does?


It works because each thread has a different stack - the two threads
won't interfere with each other, because they have separate local
variables.

--
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
Mar 19 '06 #2
Thanks Jon!

I have a solid understanding of stack and heap and I never new that
each new thread got its own stack. That really opens up the issue a
lot!

It does leave me with another question though.

Say that I have two threads running and they both read a instance
field to check if they are supposed to stop or not. How can the two
threads read that class variable if they have there own stacks?

First is this "thread safe"? If not why and is it close enough? Is
there such a thing as closed enough to being thread safe?

public class MyMainClass
{
private bool stopThreads = false;

public MyMainClass
{
Thread t1 = new Thread(new ThreadStart(DoWork));
t1.Start();

Thread t2 = new Thread(new ThreadStart(DoWork));
t2.Start();
}

private void DoWork()
{
// is accessing the class variable here "thread safe"
// and why or why not? If the thread that is running
// this method has its own stack how does it know
// where to find the stopThreads data in the heap?
// did the stack get copied when the thread started?
while (!this.stopThreads)
{
// do a lot of work here
}
}
}
On Sun, 19 Mar 2006 17:42:22 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Ryan Pedersen <pe**********@gmail.com> wrote:
I have a small program that executes a single very long running
background thread today.

The scope of all variables in the method that is running in this
thread today are all local to the method. The method doesn't do
anything outside of its local scope.

My question... can I start up a 2nd (3rd, and 4th and so on) thread
running the same method? Is that safe?


Yup, that's absolutely fine.
Looking at this I know that it is VERY simple and I would suspect that
it would work but why? Can you explain why this works, if it does?


It works because each thread has a different stack - the two threads
won't interfere with each other, because they have separate local
variables.

Mar 20 '06 #3
Ryan,

The short answers is no it is not technically thread-safe.

Read Jon's article on volatility for more information. And while
you're at it you might as well read the other articles in the series as
well. They're very informative.

<http://www.yoda.arachsys.com/csharp/threads/volatility.shtml>

Brian
Ryan Pedersen wrote:
Thanks Jon!

I have a solid understanding of stack and heap and I never new that
each new thread got its own stack. That really opens up the issue a
lot!

It does leave me with another question though.

Say that I have two threads running and they both read a instance
field to check if they are supposed to stop or not. How can the two
threads read that class variable if they have there own stacks?

First is this "thread safe"? If not why and is it close enough? Is
there such a thing as closed enough to being thread safe?

public class MyMainClass
{
private bool stopThreads = false;

public MyMainClass
{
Thread t1 = new Thread(new ThreadStart(DoWork));
t1.Start();

Thread t2 = new Thread(new ThreadStart(DoWork));
t2.Start();
}

private void DoWork()
{
// is accessing the class variable here "thread safe"
// and why or why not? If the thread that is running
// this method has its own stack how does it know
// where to find the stopThreads data in the heap?
// did the stack get copied when the thread started?
while (!this.stopThreads)
{
// do a lot of work here
}
}
}


Mar 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by adeger | last post: by
38 posts views Thread by jrlen balane | last post: by
3 posts views Thread by Elliot Rodriguez | last post: by
176 posts views Thread by nw | last post: by
17 posts views Thread by Chris M. Thomasson | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.