等待同步访问 [英] Wait for synchronized access

查看:59
本文介绍了等待同步访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个从单个线程调用的方法UpdateCache()。我还要
有一个从多个线程调用的方法GetCache()。当UpdateCache()被调用
时,正在处理缓存更新。这可能需要一些时间。


同时,多个线程中的一些线程调用了GetCache(),但因为缓存正在更新,所以

,I希望他们等待

UpdateCache()完成。


像这样的东西,用非常简单的代码:


public void UpdateCache()

{

updated = true;

try {this.data = GetData(); }

finally {updating = false; }

}


public MyCache GetCache()

{

while(updated)

Thread.Sleep(200);

返回this.data;

}


如果我使用用于获取相同功能的锁定,我想对于UpdateCache()方法将是

这样的事情:


public void UpdateCache()

{

lock(this)

{

this.data = GetData();

}

}


但是如何重写这个方法?


public MyCache GetCache()

{

?????

返回this.data;

}

我在进入同步部分之前已经看过一些使用Monitor.Enter,Monitor.Wait,Monitor.Pulse和

Monitor.Exit的示例在某个可锁定对象上

代码...但是有一些解决方案,其中多个线程只等待,

而不是锁定一个对象??


也许有索姆更好的方法来解决这个问题??

最好的问候......

Lets say I have a method UpdateCache() called from a single thread. I also
have a method GetCache() called from multiple threads. When UpdateCache() is
called, the cache updating is being processed. This can take time.

In the mean time, some of the multiple threads have called GetCache(), but
because the cache is being updated, I want them to wait for the
UpdateCache() to finish.

Something like this, in a very simplified code:

public void UpdateCache()
{
updating = true;
try { this.data = GetData(); }
finally { updating = false; }
}

public MyCache GetCache()
{
while( updating )
Thread.Sleep( 200 );
return this.data;
}

If I use lock for obtaining the same functionality, I guess it will be
something like this for the UpdateCache() method:

public void UpdateCache()
{
lock(this)
{
this.data = GetData();
}
}

But how can I rewrite this method ??

public MyCache GetCache()
{
?????
return this.data;
}

I''ve seen some examples using Monitor.Enter, Monitor.Wait, Monitor.Pulse and
Monitor.Exit on some lockable object before entering a synchronized section
of a code... but is there some solution where multiple thread only waits,
instead of locking an object ??

Maybe there are some better ways to solve this ??
Best of regards...

推荐答案

Roger,


这很简单。如果您确定GetCache和UpdateCache将在单独的线程上调用
,那么您可以这样做:

public MyCache GetCache()

{

//锁定updatecache正在处理的同一个对象。

lock(this)

//返回数据。

返回this.data;

}


public void UpdateCache()

{

//更新数据时锁定它。

锁定(这个)

//更新数据。

this.data = GetData();

}


通过在lock语句中使用相同的对象,只有一个线程在

时间可以访问该部分代码。因此,当调用UpdateCache时,如果同时调用了
GetCache,那么它将等到该段代码

被this锁定。退出了。另外,如果调用了GetCache,则在GetCache中的该部分退出之前,UpdateCache将不会调用



希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- ni ************** @ exisconsulting.com


" Roger Down" < RO ******** @ c2i.net>写在消息

新闻:嗯************** @ TK2MSFTNGP12.phx.gbl ...
Roger,

It''s very simple. If you are sure that GetCache and UpdateCache will be
called on separate threads, then you can do this:

public MyCache GetCache()
{
// Lock on the same object that updatecache is processing.
lock (this)
// Return the data.
return this.data;
}

public void UpdateCache()
{
// Lock on this while updating the data.
lock (this)
// Update the data.
this.data = GetData();
}

By using the same object in the lock statement, only one thread at a
time can access that section of code. So, when UpdateCache is called, if
GetCache is called as well, then it will wait until that section of code
locked by "this" is exited. Also, if GetCache is called, UpdateCache will
not be called until that section in GetCache is exited.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Roger Down" <ro********@c2i.net> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
让我说我有一个方法UpdateCache()从单个线程调用。我还有一个从多个线程调用的方法GetCache()。调用UpdateCache()
时,正在处理缓存更新。这可能需要一些时间。

同时,一些多线程调用了GetCache(),但是因为缓存正在更新,我希望它们等待
UpdateCache()完成。

这样的东西,在一个非常简化的代码中:

public void UpdateCache()
{
更新=是的;
尝试{this.data = GetData(); }
最后{updating = false;公共MyCache GetCache()
{
while(更新)
Thread.Sleep(200);
返回此。数据;

如果我使用lock来获得相同的功能,我想对于UpdateCache()方法将会是这样的:

public void UpdateCache()
{
lock(this)
{
this.data = GetData();
}
}

但我怎样才能重写这个方法?

公共MyCache GetCache()
返回此数据;
}
我在进入同步$ b之前看过一些使用Monitor.Enter,Monitor.Wait,Monitor.Pulse
和Monitor.Exit的示例在一些可锁定对象上$ b部分代码......但是有一些解决方案,其中多个线程只等待,而不是锁定一个对象??

也许有更好的方法来解决这个问题?

最好的问候......
Lets say I have a method UpdateCache() called from a single thread. I also
have a method GetCache() called from multiple threads. When UpdateCache() is called, the cache updating is being processed. This can take time.

In the mean time, some of the multiple threads have called GetCache(), but
because the cache is being updated, I want them to wait for the
UpdateCache() to finish.

Something like this, in a very simplified code:

public void UpdateCache()
{
updating = true;
try { this.data = GetData(); }
finally { updating = false; }
}

public MyCache GetCache()
{
while( updating )
Thread.Sleep( 200 );
return this.data;
}

If I use lock for obtaining the same functionality, I guess it will be
something like this for the UpdateCache() method:

public void UpdateCache()
{
lock(this)
{
this.data = GetData();
}
}

But how can I rewrite this method ??

public MyCache GetCache()
{
?????
return this.data;
}

I''ve seen some examples using Monitor.Enter, Monitor.Wait, Monitor.Pulse and Monitor.Exit on some lockable object before entering a synchronized section of a code... but is there some solution where multiple thread only waits,
instead of locking an object ??

Maybe there are some better ways to solve this ??
Best of regards...



Roger Down< ro ******** @ c2i.net>写道:
Roger Down <ro********@c2i.net> wrote:
让我说我有一个从单个线程调用的方法UpdateCache()。我还有一个从多个线程调用的方法GetCache()。当调用UpdateCache()时,正在处理缓存更新。这可能需要一些时间。

同时,一些多线程调用了GetCache(),但是因为缓存正在更新,我希望它们等待
UpdateCache()完成。

这样的东西,非常简单的代码:
Lets say I have a method UpdateCache() called from a single thread. I also
have a method GetCache() called from multiple threads. When UpdateCache() is
called, the cache updating is being processed. This can take time.

In the mean time, some of the multiple threads have called GetCache(), but
because the cache is being updated, I want them to wait for the
UpdateCache() to finish.

Something like this, in a very simplified code:




< snip>


首先要注意你的设计是如果GetCache被称为

然后在你稍后更新它时使用,你可能会有问题。


除此之外,我相信ReaderWriterLock就是您所追求的 - 在MSDN中找到它的
a。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



<snip>

The first thing to note with your design is that if GetCache is called
and then used while you''re later updating it, you might have problems.

Aside from that, I believe ReaderWriterLock is what you''re after - have
a look for it in MSDN.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


您应该使用监视器。显示器是专门设计的数据结构

用于此目的。


以下示例


私人布尔状态= true;


公共布尔状态


{


get


{


//锁定对象仅供此线程访问。这个

表示


//当变量状态为false时使用监视器

by

//另一个线程,第一个线程必须等待


Monitor.Enter(this);


if(status) == false)Monitor.Wait(this);


//当对象被第一个留下来的时候

//线程其他线程醒来


Monitor.Pulse(这);


//离开对象


Monitor.Exit(this);


返回状态;


}


set


{


//锁定对象


Monitor.Enter(this);


status = value;


//唤醒其他等待的步骤


Monitor.Pulse(这个);


//离开对象


Monitor.Exit(this);


}


}


" Roger Down" < RO ******** @ c2i.net>写在消息

新闻:嗯************** @ TK2MSFTNGP12.phx.gbl ...
You should use a monitor. A monitor is a data structure specially designed
for this purpose.

An example follows

private bool status=true;

public bool Status

{

get

{

//Locks the object for access only by this thread. This
means

//that when the variable status is false monitor is used
by

//another thread and the first thread has to wait

Monitor.Enter(this);

if (status==false) Monitor.Wait(this);

//When the object is left by the first

//thread other threads wake up

Monitor.Pulse(this);

//Leaves the object

Monitor.Exit(this);

return status;

}

set

{

//Locks the object

Monitor.Enter(this);

status=value;

//Wakes up the other waiting treads

Monitor.Pulse(this);

//Leaves the object

Monitor.Exit(this);

}

}

"Roger Down" <ro********@c2i.net> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
让我说我有一个方法UpdateCache()从单个线程调用。我还有一个从多个线程调用的方法GetCache()。调用UpdateCache()
时,正在处理缓存更新。这可能需要一些时间。

同时,一些多线程调用了GetCache(),但是因为缓存正在更新,我希望它们等待
UpdateCache()完成。

这样的东西,在一个非常简化的代码中:

public void UpdateCache()
{
更新=是的;
尝试{this.data = GetData(); }
最后{updating = false;公共MyCache GetCache()
{
while(更新)
Thread.Sleep(200);
返回此。数据;

如果我使用lock来获得相同的功能,我想对于UpdateCache()方法将会是这样的:

public void UpdateCache()
{
lock(this)
{
this.data = GetData();
}
}

但我怎样才能重写这个方法?

公共MyCache GetCache()
返回此数据;
}
我在进入同步$ b之前看过一些使用Monitor.Enter,Monitor.Wait,Monitor.Pulse
和Monitor.Exit的示例在一些可锁定对象上$ b部分代码......但是有一些解决方案,其中多个线程只等待,而不是锁定一个对象??

也许有更好的方法来解决这个问题?

最好的问候......
Lets say I have a method UpdateCache() called from a single thread. I also
have a method GetCache() called from multiple threads. When UpdateCache() is called, the cache updating is being processed. This can take time.

In the mean time, some of the multiple threads have called GetCache(), but
because the cache is being updated, I want them to wait for the
UpdateCache() to finish.

Something like this, in a very simplified code:

public void UpdateCache()
{
updating = true;
try { this.data = GetData(); }
finally { updating = false; }
}

public MyCache GetCache()
{
while( updating )
Thread.Sleep( 200 );
return this.data;
}

If I use lock for obtaining the same functionality, I guess it will be
something like this for the UpdateCache() method:

public void UpdateCache()
{
lock(this)
{
this.data = GetData();
}
}

But how can I rewrite this method ??

public MyCache GetCache()
{
?????
return this.data;
}

I''ve seen some examples using Monitor.Enter, Monitor.Wait, Monitor.Pulse and Monitor.Exit on some lockable object before entering a synchronized section of a code... but is there some solution where multiple thread only waits,
instead of locking an object ??

Maybe there are some better ways to solve this ??
Best of regards...



这篇关于等待同步访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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