跨线程共享内存 [英] Shared memory across threads

查看:72
本文介绍了跨线程共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个多线程项目,我有一个关于

多个线程同时使用的共享内存的问题。


我有两个独立的线程,它们有不同的回调方法。

线程一是创建一个名为mHistoricalList的ArrayList。并添加

的东西。线程2在相同的

时间循环遍历同一个列表。虽然第一个线程正在向列表添加内容,但第二个线程是
无法循环列表。程序崩溃了。


所以我在考虑使用锁定。但它不起作用 - 它给了我一个

运行时未知错误。我的代码如下(在回调方法中

线程一):


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 );

}

}


我使用锁定的方式有什么问题吗?任何人都可以告诉我如何解决这个问题吗?谢谢!

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!

推荐答案

您好,


您可以使用此处所述的ArrayList.Synchronized方法:
http://msdn.microsoft.com/en -us / library / 3azh197k.aspx
$ b $bFrédéricQueudret

CTO - Mpoware
http://www.mpoware.com/

好奇 < fi ******** @ yahoo.com写了留言

新闻:a4 ********************** ************ @ k30g2000 hse.googlegroups.com ...
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" <fi********@yahoo.comwrote in message
news:a4**********************************@k30g2000 hse.googlegroups.com...

我正在研究一个多线程项目我有一个关于

同时由多个线程使用的共享内存的问题。


我有两个独立的线程,它们有不同的回调方法。

线程一是创建一个名为mHistoricalList的ArrayList。并添加

的东西。线程2在相同的

时间循环遍历同一个列表。虽然第一个线程正在向列表添加内容,但第二个线程是
无法循环列表。程序崩溃了。


所以我在考虑使用锁定。但它不起作用 - 它给了我一个

运行时未知错误。我的代码如下(在回调方法中

线程一):


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 );

}

}


我使用锁定的方式有什么问题吗?任何人都可以告诉我如何解决这个问题吗?谢谢!
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!


Hi Frederic,


我尝试使用ArrayList的包装器。它又崩溃了。我得到了

相同的例外:


" TSF_Server_1.exe中0x7c812a5b处的未处理异常:0xC0020001:

字符串绑定是无效的。


仅供参考,我更改了以下代码:


ArrayList myThreadSafeHistoricalList =

ArrayList .Synchronized(mHistoricalList);


SymbolPriceVolume spv = new SymbolPriceVolume(自动收报机);


PriceVolume pv = new PriceVolume(openPrice,volume);

spv.PriceVolumeArray.Add(pv);

mHistoricalList.Add(spv);

我应该替换mHistoricalList吗?到处都是

" myThreadSafeHistoricalList"?我还需要使用锁定吗?
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"?


您好Frédéric,


根据您的建议,我改变了我的回调在第一个线程上如下:


void CreateHistoricalList(字符串自动收报机,双倍打开价格,双倍

卷)

{

ArrayList myThreadSafeHistoricalList =

ArrayList.Synchronized(mHistoricalList);

SymbolPriceVolume spv = new SymbolPriceVolume(ticker);

PriceVolume pv = new PriceVolume(openPrice,volume);

spv.PriceVolumeArray.Add(pv);

myThreadSafeHistoricalList.Add(spv); < br $>

}


然而,它再次因同样的未处理异常而崩溃。我做了什么

有什么不对吗?我要替换mHistoricalList吗?与

" myThreadSafeHistoricalList"在线程二的回调中

(以及其他地方)?
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)?


这篇关于跨线程共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆