C
Curious
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!
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!