线程/锁定 [英] Threading/Locking

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

问题描述

我有一个关于锁的问题,因为我看到的行为不符合我b = b的行为。我想当你在代码周围放一把锁

时,运行它的第一个实例当然会获得访问权限,然后每次访问就会获得
(当第一个实例仍在执行时)

代码)基本上会排在等待第一个

退出的锁上。但我所看到的是,最后一个是第一个得到

访问代码......这没有意义。如果有人可以放弃一些

光,我将不胜感激。下面是一个代码示例和它返回的数据

你可以看到在锁定之前记录的4行

而最后一行是第一个去的在锁内????


5/11/2005 11:26:43 AM - 锁前数据:==============

5/11/2005 11:26:43 AM - 锁前数据:==============

5/11/2005上午11:26:43 - 锁前数据:==============

5/11/2005 11:26:43 AM - 锁前数据:= ======日期

5/11/2005 11:26:43 AM - 锁定后的数据:======= DATE

5/11 / 2005 11:26:43 AM - 锁前数据:出生时间:12 /

5/11/2005 11:26:43 AM - 锁定后数据:出生时:12 /
5/11/2005 11:26:43 AM - 锁定后的数据:==============

5/11/2005 11: 26:43 AM - 锁定后的数据:==============

5/11/2005 11:26:43 AM - 锁定后的数据:=== ===========

// lockingObject是使用此方法包含在类中的私有对象

public void g etIncomingData(对象数据)

{

尝试

{

myLog.Log(string.Format("锁定前的数据:{0}",data),true);

lock(lockingObject)

{

myLog.Log(string .Format(锁定后数据:{0},数据),真);

myParser.getIncomingData((字符串)数据);

}

}

catch(例外e)

{

myLog.Log(string.Format(" DataCapture中的错误) getIncomingData:

{0}",e.Message));

}

}

解决方案

Shane< Sh *** @ discuss.microsoft.com>写道:

我有一个关于锁的问题,因为我看到的行为不符合我的正确行为。我想当你把密码放在代码周围时,运行它的第一个实例当然会获得访问权,然后每次访问后(当第一个实例仍在执行代码时)基本上排队等待第一个退出的那个锁。但我所看到的是,最后一个是第一个获得代码的访问权限......这没有任何意义。如果有人可以摆脱一些光,我会很感激。下面是一个代码示例和它返回的数据,你可以看到在锁定之前记录的4行和最后一行是第一次进入锁定的内容???? / blockquote>


没有保证订购时涉及谁获得锁定。

我不相信它被指定为是FIFO。


-

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

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


那么建议的解决办法是什么...我需要处理数据

订单,但我不想阻止发送线程,因为进入的数据

是时间敏感的(即需要快速检索)。


感谢您的帮助。


" Jon Skeet [C#MVP]"写道:

Shane< Sh *** @ discuss.microsoft.com>写道:

我有一个关于锁的问题,因为我看到的行为不符合我的正确行为。我想当你把密码放在代码周围时,运行它的第一个实例当然会获得访问权,然后每次访问后(当第一个实例仍在执行代码时)基本上排队等待第一个退出的那个锁。但我所看到的是,最后一个是第一个获得代码的访问权限......这没有任何意义。如果有人可以摆脱一些光,我会很感激。下面是一个代码示例和它返回的数据,你可以看到在锁定之前记录的4行和最后一行是第一次进入锁定的内容???? / blockquote>

没有保证订购涉及谁获得锁定。
我不相信它被指定为FIFO。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



"沙恩" <嘘*** @ discussions.microsoft.com>在消息中写道

news:59 ********************************** @ microsof t.com ...

我有一个关于锁的问题,因为我看到的行为不符合我的正确行为。我想当你在代码周围放一个
锁时,运行它的第一个实例当然会获得访问权,然后每次访问后(第一个实例仍在执行时)

代码)基本上排在等待第一个退出的锁上。但我所看到的是,最后一个是第一个获得代码的访问权限......这没有任何意义。如果有人可以摆脱一些光,我会很感激。下面是一个代码示例和它返回的数据,你可以看到在锁定之前记录的4行和最后一行是第一次进入锁定的内容???? / blockquote>


我想知道,你确定在第一次尝试之前还没有锁定对象

吗?

- Alan


I have a question about locks because I am seeing behavior that does not
match what I though was the correct behavior. I thought when you put a lock
around code the first instance that ran it would of course get access and
then each access after that (while the first instance is still executing the
code) would be essentially queued up on that lock waiting for the first to
exit. But what I am seeing is that the last one there is the first to get
access to the code... which doesn''t make sense. If someone can shed some
light I would appreciate it. Below is an example of the code and the data
that it returned you can see that 4 lines that were logged before the lock
and the last one was the first to go inside the lock????

5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ==============
5/11/2005 11:26:43 AM - Data Before Lock: ======= DATE
5/11/2005 11:26:43 AM - Data After Lock: ======= DATE
5/11/2005 11:26:43 AM - Data Before Lock: of BIRTH: 12/
5/11/2005 11:26:43 AM - Data After Lock: of BIRTH: 12/
5/11/2005 11:26:43 AM - Data After Lock: ==============
5/11/2005 11:26:43 AM - Data After Lock: ==============
5/11/2005 11:26:43 AM - Data After Lock: ==============
//lockingObject is a private object contained in the class with this method
public void getIncomingData(object data)
{
try
{
myLog.Log(string.Format("Data Before Lock: {0}",data),true);
lock(lockingObject)
{
myLog.Log(string.Format("Data After Lock: {0}",data),true);
myParser.getIncomingData((string)data);
}
}
catch(Exception e)
{
myLog.Log(string.Format("Error in DataCapture getIncomingData:
{0}",e.Message));
}
}

解决方案

Shane <Sh***@discussions.microsoft.com> wrote:

I have a question about locks because I am seeing behavior that does not
match what I though was the correct behavior. I thought when you put a lock
around code the first instance that ran it would of course get access and
then each access after that (while the first instance is still executing the
code) would be essentially queued up on that lock waiting for the first to
exit. But what I am seeing is that the last one there is the first to get
access to the code... which doesn''t make sense. If someone can shed some
light I would appreciate it. Below is an example of the code and the data
that it returned you can see that 4 lines that were logged before the lock
and the last one was the first to go inside the lock????



There''s no guaranteed ordering involving in who acquires the lock when.
I don''t believe it''s specified to be FIFO.

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


So what is the suggested solution to this...I need the data to be handled in
order, but I don''t want to have to block the sending thread because the data
coming in is time sensitive (ie needs to be retrieved quickly).

thanks for your help.

"Jon Skeet [C# MVP]" wrote:

Shane <Sh***@discussions.microsoft.com> wrote:

I have a question about locks because I am seeing behavior that does not
match what I though was the correct behavior. I thought when you put a lock
around code the first instance that ran it would of course get access and
then each access after that (while the first instance is still executing the
code) would be essentially queued up on that lock waiting for the first to
exit. But what I am seeing is that the last one there is the first to get
access to the code... which doesn''t make sense. If someone can shed some
light I would appreciate it. Below is an example of the code and the data
that it returned you can see that 4 lines that were logged before the lock
and the last one was the first to go inside the lock????



There''s no guaranteed ordering involving in who acquires the lock when.
I don''t believe it''s specified to be FIFO.

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



"Shane" <Sh***@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...

I have a question about locks because I am seeing behavior that does not
match what I though was the correct behavior. I thought when you put a
lock
around code the first instance that ran it would of course get access and
then each access after that (while the first instance is still executing
the
code) would be essentially queued up on that lock waiting for the first to
exit. But what I am seeing is that the last one there is the first to get
access to the code... which doesn''t make sense. If someone can shed some
light I would appreciate it. Below is an example of the code and the data
that it returned you can see that 4 lines that were logged before the lock
and the last one was the first to go inside the lock????



I was wondering, are you sure there is not already a lock on lockingobject
before that first try?

-- Alan


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

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