Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Shared memory across threads

Reply
 
Old 06-18-2008   #1 (permalink)
Curious


 
 

Shared memory across threads

I'm working on a multi-threading project and I've got an issue about
shared memory used by multiple threads at the same time.

I have two separate threads that have different callback methods.
Thread one is to create an ArrayList called "mHistoricalList" and add
things to it. Thread two is looping through the same list at the same
time. While thread one is adding things to the list, thread two is
unable to loop the list. So the program crashed.

So I was thinking of using "lock". But it doesn't work - It gives me a
runtime unknown error. My code is below (in the callback methond on
thread one):

void CreateHistoricalList (string ticker, double openPrice, double
volume)
{
lock (mHistoricalList)
{
SymbolPriceVolume spv = new SymbolPriceVolume(ticker);
PriceVolume pv = new PriceVolume(openPrice, volume);
spv.PriceVolumeArray.Add(pv);
mHistoricalList.Add(spv);
}
}

Is there anything wrong with the way I use the "lock"? Could anyone
advise me on how I can get this fixed? Thanks!

My System SpecsSystem Spec
Old 06-18-2008   #2 (permalink)
Frédéric Queudret


 
 

Re: Shared memory across threads

Hi,

You may use the ArrayList.Synchronized method as described here:
http://msdn.microsoft.com/en-us/library/3azh197k.aspx

Frédéric Queudret
CTO - Mpoware
http://www.mpoware.com/

"Curious" <fir5tsight@xxxxxx> wrote in message
news:a42f367f-70a2-4ce1-8585-d35c128944fa@xxxxxx
Quote:

> I'm working on a multi-threading project and I've got an issue about
> shared memory used by multiple threads at the same time.
>
> I have two separate threads that have different callback methods.
> Thread one is to create an ArrayList called "mHistoricalList" and add
> things to it. Thread two is looping through the same list at the same
> time. While thread one is adding things to the list, thread two is
> unable to loop the list. So the program crashed.
>
> So I was thinking of using "lock". But it doesn't work - It gives me a
> runtime unknown error. My code is below (in the callback methond on
> thread one):
>
> void CreateHistoricalList (string ticker, double openPrice, double
> volume)
> {
> lock (mHistoricalList)
> {
> SymbolPriceVolume spv = new SymbolPriceVolume(ticker);
> PriceVolume pv = new PriceVolume(openPrice, volume);
> spv.PriceVolumeArray.Add(pv);
> mHistoricalList.Add(spv);
> }
> }
>
> Is there anything wrong with the way I use the "lock"? Could anyone
> advise me on how I can get this fixed? Thanks!
My System SpecsSystem Spec
Old 06-18-2008   #3 (permalink)
Curious


 
 

Re: Shared memory across threads

Hi Frederic,

I tried with the wrapper of ArrayList. It crashed again. I got the
same exception:

"Unhandled exception at 0x7c812a5b in TSF_Server_1.exe: 0xC0020001:
The string binding is invalid."

FYI, I changed the code as below:

ArrayList myThreadSafeHistoricalList =
ArrayList.Synchronized(mHistoricalList);

SymbolPriceVolume spv = new SymbolPriceVolume(ticker);

PriceVolume pv = new PriceVolume(openPrice, volume);
spv.PriceVolumeArray.Add(pv);
mHistoricalList.Add(spv);


Shall I replace "mHistoricalList" everywhere with
"myThreadSafeHistoricalList"? Do I still need to use "lock"?
My System SpecsSystem Spec
Old 06-18-2008   #4 (permalink)
Curious


 
 

Re: Shared memory across threads

Hi Frédéric,

According to your suggestion, I changed my callback on thread one as
below:

void CreateHistoricalList (string ticker, double openPrice, double
volume)
{
ArrayList myThreadSafeHistoricalList =
ArrayList.Synchronized(mHistoricalList);
SymbolPriceVolume spv = new SymbolPriceVolume(ticker);
PriceVolume pv = new PriceVolume(openPrice, volume);
spv.PriceVolumeArray.Add(pv);
myThreadSafeHistoricalList.Add(spv);

}

However, it crashed again for the same unhandled exception. Did I do
anything wrong? Shall I replace "mHistoricalList" with
"myThreadSafeHistoricalList" in the callback on thread two as well
(and everywhere else)?


My System SpecsSystem Spec
Old 06-18-2008   #5 (permalink)
Adam Benson


 
 

Re: Shared memory across threads

Hi,

Leave thread one as it is.

Thread two should look something like this :

lock (mHistoricalList)
{
foreach (SymbolPriceVolume spv in mHistoricalList)
{
// Do stuff here
}
}

Are you locking your list for the whole time you iterate over it?

If one thread is enumerating a list and another thread alters the list
you're going to have one unhappy computer on your hands.

On another note I'd encourage you to take a step back and ask why you need 2
threads accessing a list in this way. Enumerating a list can be a long time
to lock the whole thing down.

- Adam.
==============


My System SpecsSystem Spec
Old 06-18-2008   #6 (permalink)
Frédéric Queudret


 
 

Re: Shared memory across threads

Hi,

I tried to reproduce your problem and finally it worked fine for me using a
lock on the SyncRoot property of the ArrayList instance.
The code I used to reproduce is the following one.
Let me know if it solvees your problem:
public class Class1
{
private ArrayList _items;
private Thread _t1;
private Thread _t2;

public Class1()
{
_items = new ArrayList();
}

public void Execute()
{
_t2 = new Thread(new ThreadStart(ShowItems));
_t1 = new Thread(new ThreadStart(AddItems));
_t1.Start();
_t2.Start();
}

public void ShowItems()
{
while (true)
{
//ArrayList synchItems = ArrayList.Synchronized(_items); <-
doesn't work
lock (_items.SyncRoot)
{
foreach (object item in _items)
{
Console.WriteLine((string)item);

}
}

}
}
public void AddItems()
{
int cpt = 0;
while (true)
{
//ArrayList synchItems = ArrayList.Synchronized(_items); <-
does not work
lock (_items.SyncRoot)
{
_items.Add("CPT" + cpt.ToString());
}
cpt++;

}
}
}

Frédéric

"Curious" <fir5tsight@xxxxxx> wrote in message
news:7545b895-1a2b-4337-951d-db3efcb817ba@xxxxxx
Hi Frédéric,

According to your suggestion, I changed my callback on thread one as
below:

void CreateHistoricalList (string ticker, double openPrice, double
volume)
{
ArrayList myThreadSafeHistoricalList =
ArrayList.Synchronized(mHistoricalList);
SymbolPriceVolume spv = new SymbolPriceVolume(ticker);
PriceVolume pv = new PriceVolume(openPrice, volume);
spv.PriceVolumeArray.Add(pv);
myThreadSafeHistoricalList.Add(spv);

}

However, it crashed again for the same unhandled exception. Did I do
anything wrong? Shall I replace "mHistoricalList" with
"myThreadSafeHistoricalList" in the callback on thread two as well
(and everywhere else)?


My System SpecsSystem Spec
Old 06-18-2008   #7 (permalink)
Rob Cooke


 
 

RE: Shared memory across threads

Jon Skeet has a fairly simple demonstration of a producer-consumer
relationship with monitors here:
http://www.yoda.arachsys.com/csharp/...eadlocks.shtml

It may not be exactly what you're looking for but, I found it of good value
when I started exploring synchronization a bit more.


"Curious" wrote:
Quote:

> I'm working on a multi-threading project and I've got an issue about
> shared memory used by multiple threads at the same time.
>
> I have two separate threads that have different callback methods.
> Thread one is to create an ArrayList called "mHistoricalList" and add
> things to it. Thread two is looping through the same list at the same
> time. While thread one is adding things to the list, thread two is
> unable to loop the list. So the program crashed.
>
> So I was thinking of using "lock". But it doesn't work - It gives me a
> runtime unknown error. My code is below (in the callback methond on
> thread one):
>
> void CreateHistoricalList (string ticker, double openPrice, double
> volume)
> {
> lock (mHistoricalList)
> {
> SymbolPriceVolume spv = new SymbolPriceVolume(ticker);
> PriceVolume pv = new PriceVolume(openPrice, volume);
> spv.PriceVolumeArray.Add(pv);
> mHistoricalList.Add(spv);
> }
> }
>
> Is there anything wrong with the way I use the "lock"? Could anyone
> advise me on how I can get this fixed? Thanks!
>
My System SpecsSystem Spec
Old 06-18-2008   #8 (permalink)
Curious


 
 

Re: Shared memory across threads

The callback method is huge and long on thread two. If I use "lock" in
it, it'll block other thread for a long time.

Question: If I use "lock", shall I use it in both the callback methods
on both threads?
My System SpecsSystem Spec
Old 06-18-2008   #9 (permalink)
Curious


 
 

Re: Shared memory across threads

Hi Frédéric,

Thanks for showing me working code.

However, your sample code doesn't work for my program, because the
ArrayList is altered in both the callback method on thread one, and
the callback method on thread two.

Thread one is adding new objects to the ArrayList, while thread two is
looping through the list and altering some objects already on the
list.

In your case, thread one is adding new objects but thread two is
looping the list without changing the list. In other words, thread one
is "write" operation while thread two is "read" operation.

In my case, both threads are "write" operations.
My System SpecsSystem Spec
Old 06-19-2008   #10 (permalink)
Frédéric Queudret


 
 

Re: Shared memory across threads

Ok, but it does work too on my sample if you alter the ArrayList on the
second thread.
The sample code is:

public void ShowAndAlterItems()
{
while (true)
{
lock (_items.SyncRoot)
{
_items[_items.Count - 1] = _items[_items.Count - 1] + "
altered from t2";
foreach (object item in _items)
{
Console.WriteLine((string)item);
}
// add a new item
_items.Add("Hello From Thread 2");
}

}
}
public void AddItems()
{
int cpt = 0;
while (true)
{
lock (_items.SyncRoot)
{
_items.Add("CPT" + cpt.ToString());
}
cpt++;
}
}

Do you have any code that show the issue here?
Frédéric

"Curious" <fir5tsight@xxxxxx> wrote in message
news:d017f4a0-7afc-4995-a5c2-88978f917017@xxxxxx
Hi Frédéric,

Thanks for showing me working code.

However, your sample code doesn't work for my program, because the
ArrayList is altered in both the callback method on thread one, and
the callback method on thread two.

Thread one is adding new objects to the ArrayList, while thread two is
looping through the list and altering some objects already on the
list.

In your case, thread one is adding new objects but thread two is
looping the list without changing the list. In other words, thread one
is "write" operation while thread two is "read" operation.

In my case, both threads are "write" operations.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Dedicated Graphical Memory & Shared System Memory Vista hardware & devices
Shared Memory Vista performance & maintenance
problem with memory (graphics shared memory) Vista General
Ati Radeon 9550 using system memory? 511mb shared system memory. Vista hardware & devices


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46