System.Threading.Thread Question

T

Trecius

Hello, Newsgroupians:

I've two questions regarding the System.Threading.Thread namespace.

First, what is the point of having a ThreadStart parameter? For example...

using System.Threading;
Thread myThread;

myThread = new Thread(new ThreadStart(myFunc)); // This works
myThread = new Thread(myFunc); // And this works

Why not just make it easy and always use the second calling?

My second question is slightly more detailed.

I have a thread that I have created in a class...

using System.Threading;

public struct MyPoint
{
public float x;
public float y;
}

Thread m_myThread;
MyPoint m_pt;

class MyClass
{
void ThreadStartFunc()
{
m_pt.x = 1;
m_pt.y = 2;
m_myThread = new Thread(this.SomeThreadFunc);
}
void SomeThreadFunc()
{
m_pt.x = 5;
m_pt.y = 6;
}
}

Now suppose I call ThreadStartFunc(); in it, I initialize x and y. When I
debug, I notice the value of x and y are INITIALLY 1 and 2 in the
SomeThreadFunc(). Later in the thread I change the values, and the thread
terminates. However, when looking at the values in the main thread, I notice
that the values are still 1 and 2. Can someone explain why this is
happening? Thank you for your assistance.


Trecius
 
J

Jon Skeet [C# MVP]

Trecius said:
I've two questions regarding the System.Threading.Thread namespace.

First, what is the point of having a ThreadStart parameter? For example...

using System.Threading;
Thread myThread;

myThread = new Thread(new ThreadStart(myFunc)); // This works
myThread = new Thread(myFunc); // And this works

Why not just make it easy and always use the second calling?

The second is just shorthand for the first - it's the same type of
parameter. The syntax in the second form is only available as of C# 2.
My second question is slightly more detailed.

I have a thread that I have created in a class...

using System.Threading;

public struct MyPoint
{
public float x;
public float y;
}

Thread m_myThread;
MyPoint m_pt;

class MyClass
{
void ThreadStartFunc()
{
m_pt.x = 1;
m_pt.y = 2;
m_myThread = new Thread(this.SomeThreadFunc);
}
void SomeThreadFunc()
{
m_pt.x = 5;
m_pt.y = 6;
}
}

Now suppose I call ThreadStartFunc(); in it, I initialize x and y. When I
debug, I notice the value of x and y are INITIALLY 1 and 2 in the
SomeThreadFunc(). Later in the thread I change the values, and the thread
terminates. However, when looking at the values in the main thread, I notice
that the values are still 1 and 2. Can someone explain why this is
happening? Thank you for your assistance.

See http://pobox.com/~skeet/csharp/threads/volatility.shtml
 
M

Marc Gravell

The second question depends on a few things that aren't clear from
your sample, since your code is invalid... mainly *exactly* how m_pt
is handled. In your code it hangs off the namespace, which is illegal.
However, since it is a struct it will follow value-type semantics, so
normally (when benig handed around) it will be cloned. Hence changes
are local to each individual clone, and a good illustration of why
mutable structs are dangerously confusing.

If (in your real code) m_pt is actually a field on an instance that
both can se, it is possible that it *is* updated directly, but if
there is a property in the way it will be cloned... there are also
some scenarios involving "captured variables" that could add
confusion.

Bottom line: unless you really, really know what you are doing (and
why), stick to classes or immutable structs. In this case (point) an
immutable struct seems entirely appropriate.

Marc
 

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