关于哈希表的锁定问题 [英] Locking Question around hashtable

查看:107
本文介绍了关于哈希表的锁定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在csharp中,读取和写入

哈希表的正确锁定是什么。请注意,阅读器没有循环键,

只需用特定的键读取项目:


如果我有以下哈希表h多个读者和1

作者(在不同的线程上)这是正确的锁定:


[读者]

锁定( h.syncroot)

{

string r = h [" Test"];

}

msgbox r;


[作家]

string w =" Mike" ;;

lock(h.syncroot)

{

h [" Test"] = w;

}

In csharp, what is the correct locking around reading and writing into
a hashtable. Note that the reader is not looping through the keys,
simply reading an item out with a specific key:

If i have the following hashtable h which has multiple readers and 1
writer (on different threads) is this the correct locking below:

[Reader]
lock (h.syncroot)
{
string r = h["Test"];
}
msgbox r;

[Writer]
string w = "Mike";
lock (h.syncroot)
{
h["Test"] = w;
}

推荐答案

ak*********@hotmail.com 写道:
在csharp中,读取和写入哈希表的正确锁定是什么。请注意,读者没有循环键,只需使用特定键读取项目:

如果我有以下哈希表h,其中有多个读者和1
编写器(在不同的线程上)这是下面正确的锁定:
In csharp, what is the correct locking around reading and writing into
a hashtable. Note that the reader is not looping through the keys,
simply reading an item out with a specific key:

If i have the following hashtable h which has multiple readers and 1
writer (on different threads) is this the correct locking below:




< snip>


如果你已经只有一个作家和多个读者,

文档说明你不需要任何锁定。但是,如果你想确保

a读者总能看到作家的最后一次写作,我怀疑你*需要锁定*


您可以使用ReaderWriterLock,虽然其他人使用

使用了这个(至少在1.1中 - 不确定它是否已在2.0中修复)

说ReaderWriterLock是如此之慢以至于通常要快得多

每次都要进行独占锁定(在这种情况下你的代码是

罚款。


Jon



<snip>

If you''ve just got a single writer and multiple readers, the
documentation states that you don''t need any locking. However, I
suspect you *do* need locking if you want to make absolutely sure that
a reader will always see a writer''s last write.

You could use a ReaderWriterLock for this, although others who have
used this (at least in 1.1 - not sure if it''s fixed in 2.0 or not) have
said that ReaderWriterLock is so slow that it''s usually much faster
just to take an exclusive lock every time (in which case your code is
fine).

Jon


感谢您的回复。 。另一个后续问题:


假设我有哈希表的哈希表(再次在多个

线程中有一个编写器和多个读者。) )


下面代码的正确锁定是什么。


[读者]

Hashtable n = h [" Test''];

string Name = n [" Joe"];


[作家]

hash.Add(" Joe",true);

h [" Test"] = hash;

Jon Skeet [C#MVP]写道:
thanks for the response . .heres another followup question:

Lets say i have have a hashtable of hashtables (again on multiple
threads where there is one writer and multiple readers . .)

what would be the correct locking around the code below.

[Reader]
Hashtable n = h["Test''];
string Name = n["Joe"];

[Writer]
hash.Add("Joe", true);
h["Test"] = hash;
Jon Skeet [C# MVP] wrote:
ak*********@hotmail.com 写道:
在csharp中,读取和写入哈希表的正确锁定是什么。请注意,读者没有循环键,只需使用特定键读取项目:

如果我有以下哈希表h,其中有多个读者和1
编写器(在不同的线程上)这是正确的锁定:
In csharp, what is the correct locking around reading and writing into
a hashtable. Note that the reader is not looping through the keys,
simply reading an item out with a specific key:

If i have the following hashtable h which has multiple readers and 1
writer (on different threads) is this the correct locking below:



< snip>

如果你只有一个作家和多个读者,
文档说明你不需要任何锁定。但是,我怀疑你*需要锁定,如果你想确保读者总能看到作家的最后一次写作。

你可以使用一个ReaderWriterLock为此,虽然其他人已经使用过这个(至少在1.1中 - 不确定它是否已在2.0中修复)
表示ReaderWriterLock是如此之慢以至于它是通常要快得多
每次都要独占锁(在这种情况下你的代码很好)。

Jon



<snip>

If you''ve just got a single writer and multiple readers, the
documentation states that you don''t need any locking. However, I
suspect you *do* need locking if you want to make absolutely sure that
a reader will always see a writer''s last write.

You could use a ReaderWriterLock for this, although others who have
used this (at least in 1.1 - not sure if it''s fixed in 2.0 or not) have
said that ReaderWriterLock is so slow that it''s usually much faster
just to take an exclusive lock every time (in which case your code is
fine).

Jon






我推荐(直到J Skeet没有来批评我:))使用ReaderWriter

锁定如果只有一个作家和几个读者


使用ReaderWriterLock类,任意数量的线程都可以安全地同时读取数据

。仅在线程更新时才锁定数据。读者主题

只有在没有持有锁定的作者时才能获得锁定。作家

线程只有在没有读者或作者持有

锁定时才能获得锁定。


公共类ReadWrite

{

私人ReaderWriterLock rwl;

私人HashTable h;


public ReadWrite()

{

rwl = new ReaderWriterLock();

}


public void ReadInts(ref int a, ref int b)

{

rwl.AcquireReaderLock(Timeout.Infinite);

尝试

{

string r = h [" Test"];

}

终于

{

rwl.ReleaseReaderLock();

}

}


public void WriteInts(int a,int b)

{

rwl.AcquireWriterLock(Timeout.Infinite);


试试

{

string w =" Mike";

h [" Test"] = w;

}

finally

{

rwl.ReleaseWriterLock();

}

}

}


" ak ********* @ hotmail.com"写道:
I recomend (till J Skeet did''t come and criticise me :)) to use ReaderWriter
lock if only one writer and several readers

Using the ReaderWriterLock class, any number of threads can safely read data
concurrently. Only when threads are updating is data locked. Reader threads
can acquire a lock only if there are no writers holding the lock. Writer
threads can acquire lock only if there are no readers or writers holding the
lock.

public class ReadWrite
{
private ReaderWriterLock rwl;
private HashTable h;

public ReadWrite()
{
rwl = new ReaderWriterLock();
}

public void ReadInts(ref int a, ref int b)
{
rwl.AcquireReaderLock(Timeout.Infinite);
try
{
string r = h["Test"];
}
finally
{
rwl.ReleaseReaderLock();
}
}

public void WriteInts(int a, int b)
{
rwl.AcquireWriterLock(Timeout.Infinite);

try
{
string w = "Mike";
h["Test"] = w;
}
finally
{
rwl.ReleaseWriterLock();
}
}
}

"ak*********@hotmail.com" wrote:
在csharp中,读取和写入哈希表的正确锁定是什么。请注意,读者没有循环键,只需使用特定键读取项目:

如果我有以下哈希表h,其中有多个读者和1
writer(在不同的线程上)这是正确的锁定:
In csharp, what is the correct locking around reading and writing into
a hashtable. Note that the reader is not looping through the keys,
simply reading an item out with a specific key:

If i have the following hashtable h which has multiple readers and 1
writer (on different threads) is this the correct locking below:




-

WBR,

迈克尔Nemtsev :: blog: http://spaces.msn.com/laflour


有时一个人仍忠于某一事业,只因为它的对手不会因为b $ b不再是平淡的。 (c)Friedrich Nietzsche



--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


这篇关于关于哈希表的锁定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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