473,320 Members | 1,853 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,320 software developers and data experts.

Threading and Instances

Hello, Newsgroupians:

I'm a little confused when it comes to threads in C#. Here's what I'm
trying to do...

I have a List<containing a set of MyPoint(s)...

public struct MyPoint
{
int x;
int y;
int val;
}

class MyClass
{
List<MyPointm_list = new List<MyPoint>();
...
}

Now, in my main thread I'm going to populate the list. Assume the
following...

class MyClass
{
public MyClass()
{
for (int i = 0; i < 10; i++)
{
MyPoint pt = new MyPoint;
pt.x = <Some X>
pt.y = <Some Y>
// pt.val is changed in the thread

m_list.Add(pt);
}
}
...
}

Now, in the secondary thread, I am going to be changing .val
For simplicity, assume I do the following...

class MyClass
{
void SomeFunc()
{
Thread th = new Thread(new ThreadStart(this.ThreadFunc));
th.Start();
while (th.IsAlive) // Wait until the secondary thread is done
{
Thread.Sleep(1000);
}

for (int i = 0; i < m_list.Count; i++)
{
Console.WriteLine(m_list[i].val);
}
}

public void ThreadFunc()
{
Random r = new Random();
for (int i = 0; i < this.m_list.Count; i++)
{
MyPoint pt = this.m_list[i];
pt.val = r.Next();
}
...
}
}

Yet, even after waiting for the secondary thread to end, the .val for each
MyPoint in the List<is always zero. I don't understand why it isn't
changing. I've tried setting some variables to static; this didn't work.

Overall, what do I need to do to make it so the secondary thread can write
to the values in the List<and have the main thread read these values once
the thread has ended? Thank you.
Trecius
Nov 30 '07 #1
6 967
The answer is that MyPoint is a struct; it has value-type semantics.
Every time you get it from the list you get a *different* copy.

Aside: there isn't any locking, but you've guarded against this OK
because the two threads don't run at the same time - but be aware that
there are some register issues which might stop loops exiting -
and .Join() would be more efficient anyway.

But more importantly:

th.Start();
while (th.IsAlive) // Wait until the secondary thread is done
{
Thread.Sleep(1000);
}
Why start a thread, just to wait for it to end... run the code
directly ;-p

Marc
Nov 30 '07 #2
And can I point out that I already told you this?

http://groups.google.co.uk/group/mic...79abc1c12a8bba

Marc
Nov 30 '07 #3
Mr. Gravell:

So should my struct be a class?
Trecius

"Marc Gravell" wrote:
The answer is that MyPoint is a struct; it has value-type semantics.
Every time you get it from the list you get a *different* copy.

Aside: there isn't any locking, but you've guarded against this OK
because the two threads don't run at the same time - but be aware that
there are some register issues which might stop loops exiting -
and .Join() would be more efficient anyway.

But more importantly:

th.Start();
while (th.IsAlive) // Wait until the secondary thread is done
{
Thread.Sleep(1000);
}
Why start a thread, just to wait for it to end... run the code
directly ;-p

Marc
Nov 30 '07 #4
Hi,
Just change MyPoint from a struct to a class and run the test again.

Take a look at Jon Skeet's comment regarding value vs reference types

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Trecius" <Tr*****@discussions.microsoft.comwrote in message
news:27**********************************@microsof t.com...
Hello, Newsgroupians:

I'm a little confused when it comes to threads in C#. Here's what I'm
trying to do...

I have a List<containing a set of MyPoint(s)...

public struct MyPoint
{
int x;
int y;
int val;
}

class MyClass
{
List<MyPointm_list = new List<MyPoint>();
...
}

Now, in my main thread I'm going to populate the list. Assume the
following...

class MyClass
{
public MyClass()
{
for (int i = 0; i < 10; i++)
{
MyPoint pt = new MyPoint;
pt.x = <Some X>
pt.y = <Some Y>
// pt.val is changed in the thread

m_list.Add(pt);
}
}
...
}

Now, in the secondary thread, I am going to be changing .val
For simplicity, assume I do the following...

class MyClass
{
void SomeFunc()
{
Thread th = new Thread(new ThreadStart(this.ThreadFunc));
th.Start();
while (th.IsAlive) // Wait until the secondary thread is done
{
Thread.Sleep(1000);
}

for (int i = 0; i < m_list.Count; i++)
{
Console.WriteLine(m_list[i].val);
}
}

public void ThreadFunc()
{
Random r = new Random();
for (int i = 0; i < this.m_list.Count; i++)
{
MyPoint pt = this.m_list[i];
pt.val = r.Next();
}
...
}
}

Yet, even after waiting for the secondary thread to end, the .val for each
MyPoint in the List<is always zero. I don't understand why it isn't
changing. I've tried setting some variables to static; this didn't work.

Overall, what do I need to do to make it so the secondary thread can write
to the values in the List<and have the main thread read these values
once
the thread has ended? Thank you.
Trecius

Nov 30 '07 #5
Yes,

A good rule of thumb is to ALWAYS use classes in your design.
structs are not the same as in C++.

Make sure that you fully understand all of the subtle ramifications of
using structs in your design before attempting to do so. Most of the
time you will find that classes are a far better choice.

Bill
"Trecius" <Tr*****@discussions.microsoft.comwrote in message
news:FB**********************************@microsof t.com...
Mr. Gravell:

So should my struct be a class?
Trecius

"Marc Gravell" wrote:
>The answer is that MyPoint is a struct; it has value-type semantics.
Every time you get it from the list you get a *different* copy.

Aside: there isn't any locking, but you've guarded against this OK
because the two threads don't run at the same time - but be aware
that
there are some register issues which might stop loops exiting -
and .Join() would be more efficient anyway.

But more importantly:

th.Start();
while (th.IsAlive) // Wait until the secondary thread is done
{
Thread.Sleep(1000);
}
Why start a thread, just to wait for it to end... run the code
directly ;-p

Marc

Nov 30 '07 #6
Since several people already answered the question, the other question would
be, why won't the compiler issue a warning such as:

The private field 'pt' is assigned but its value is never used.

And then add the squiggly line underneath the variable to make this very
obvious. This would be nice since people always run into scenarios like
this.

I mean, I realize that the "ThreadFunc()" function has the "pt.val =
r.Next()" line that uses the 'pt' variable but there is only assignments to
it right? the "ThreadFunc()" never really extracts a value from 'pt' so my
guess is that the warning would hold true.

No? Yes? Maybe?


"Trecius" <Tr*****@discussions.microsoft.comwrote in message
news:27**********************************@microsof t.com...
Hello, Newsgroupians:

I'm a little confused when it comes to threads in C#. Here's what I'm
trying to do...

I have a List<containing a set of MyPoint(s)...

public struct MyPoint
{
int x;
int y;
int val;
}

class MyClass
{
List<MyPointm_list = new List<MyPoint>();
...
}

Now, in my main thread I'm going to populate the list. Assume the
following...

class MyClass
{
public MyClass()
{
for (int i = 0; i < 10; i++)
{
MyPoint pt = new MyPoint;
pt.x = <Some X>
pt.y = <Some Y>
// pt.val is changed in the thread

m_list.Add(pt);
}
}
...
}

Now, in the secondary thread, I am going to be changing .val
For simplicity, assume I do the following...

class MyClass
{
void SomeFunc()
{
Thread th = new Thread(new ThreadStart(this.ThreadFunc));
th.Start();
while (th.IsAlive) // Wait until the secondary thread is done
{
Thread.Sleep(1000);
}

for (int i = 0; i < m_list.Count; i++)
{
Console.WriteLine(m_list[i].val);
}
}

public void ThreadFunc()
{
Random r = new Random();
for (int i = 0; i < this.m_list.Count; i++)
{
MyPoint pt = this.m_list[i];
pt.val = r.Next();
}
...
}
}

Yet, even after waiting for the secondary thread to end, the .val for each
MyPoint in the List<is always zero. I don't understand why it isn't
changing. I've tried setting some variables to static; this didn't work.

Overall, what do I need to do to make it so the secondary thread can write
to the values in the List<and have the main thread read these values
once
the thread has ended? Thank you.
Trecius

Nov 30 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Carl Waldbieser | last post by:
I have been considering using Python and the Reportlab library for generating PDF reports for the back-end of a web based application. The application runs most of its background tasks on a...
10
by: Danny Carvajal | last post by:
Hello, I have an application that uses 5 threads. So I simply create the 5 threads. However, I need to be able to create x number of threads at a time. Not sure how to do this. Any links to...
4
by: Phillip N Rounds | last post by:
I'm cursed by being a linear thinker. Anyway, I have an application (service) where I can't decide on the appropriate way to implement threading. I have a timer which periodically initiates...
2
by: Howard Swope | last post by:
Could someone help explain thread safety issues in the System.Collections classes? The documentation states:...
13
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that....
5
by: microsoft | last post by:
Just researching threading and I'm sure we need to do more review of available resources BUT I'm wondering if what we're looking for is actually possible. If it is we can keep reading. I know...
11
by: Steve Smith | last post by:
I have written a winforms application that launches approximately 150 threads with Thread.ThreadStart() Each thread uses CDO 1.21 to logon to a different Exchange mailbox and send/receive a...
0
by: juxstapose | last post by:
Hello, I have been develop a blocking socket application with threading. The main thread handles connections and inserts them into python's protected queue as jobs for the thread pool to handle....
2
by: =?Utf-8?B?TWljaGFlbCBNYWVz?= | last post by:
Hello, I have an Mdi-application which also is a remoting-server (Ipc). Several instances can co-exist on the same machine. I have a small app (search-engine) (remoting-client) which calls a...
2
by: Jeroen | last post by:
Hi all, I've been trying to get my head around threading. Here's an example from the book I'm reading: /***************************/ Mutex m = null; const string name = "xyz"; try
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.