发现Bug:Fun Concurrency Bug [英] Spot the Bug: Fun Concurrency Bug

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

问题描述

我昨晚在我写的一些测试代码中遇到了这个bug。花了几分钟b $ b分钟来弄清楚根本原因是什么,它让我思考,

哇。这是一个有趣的,并不经常出现!


所以,为了好玩,谁看到了这个bug并且可以解释它为什么会发生?


List< Threadthreads = new List< Thread>();

for(int i = 0; i< 2; i ++)

{

线程t =新线程(委托()

{

Debug.Assert(i!= 2);

});


t.Start();

threads.Add(t);

}


foreach(线程中的线程)

thread.Join();


-

Chris Mullins

I hit this bug last night in some test code I had written. It took a few
minutes to figure out what the root cause was, and it left me thinking,
"Wow. That was an interesting one that doesn''t come up very often!".

So, for fun, who sees the bug and can explain why it''s happening?

List<Threadthreads = new List<Thread>();
for (int i = 0; i < 2; i++)
{
Thread t = new Thread(delegate()
{
Debug.Assert(i != 2);
});

t.Start();
threads.Add(t);
}

foreach (Thread thread in threads)
thread.Join();

--
Chris Mullins

推荐答案

Chris Mullins [MVP - C#]写道:
Chris Mullins [MVP - C#] wrote:

我昨晚在我编写的一些测试代码中遇到了这个bug。花了几分钟b $ b分钟来弄清楚根本原因是什么,它让我思考,

哇。这是一个有趣的,并不经常出现!


所以,为了好玩,谁看到了这个bug并且可以解释它为什么会发生?
I hit this bug last night in some test code I had written. It took a few
minutes to figure out what the root cause was, and it left me thinking,
"Wow. That was an interesting one that doesn''t come up very often!".

So, for fun, who sees the bug and can explain why it''s happening?



定义bug。


假设您的主线程在创建

个线程,两个线程都将使断言失败。而且由于

主线程做得很少,实际上很可能会在另一个线程运行之前第一次调用Join()时得到



但这是一个错误吗? Debug类是线程安全的,所以有两个

线程并发失败的断言不应该导致问题和

本身。


Pete

Define "bug".

Assuming your main thread doesn''t get preempted while creating the
threads, both threads are going to fail the assertion. And since the
main thread is doing so little, it is in fact likely to get all the way
to the first call to Join() before another thread gets to run.

But is that a bug? The Debug class is thread-safe, so having two
threads concurrent fail an assertion shouldn''t cause a problem in and of
itself.

Pete


即使它确实处理并发问题,我也不会在

并发问题下提交这个问题,本身。该错误来自于在循环中使用匿名方法

并捕获变量i。


因为它用于匿名方法(和它只有一个

实例创建),当循环退出时,我等于2.并非所有的

线程都已经启动了这一点,然后当他们这样做时,

断言失败。

-

- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com


Chris Mullins [MVP - C#]" < cm ****** @ yahoo.com在留言中写道

新闻:e6 ************** @ TK2MSFTNGP02.phx.gbl ...
Even though it does deal with concurrency, I wouldn''t file this under a
concurrency issue, per se. The error comes from using an anonymous method
in the loop and capturing the variable "i".

Because it is used in the anonymous method (and there is only one
instance of it created), when the loop exits, i is equal to 2. Not all the
threads have started up by this point, and then by the time that they do,
the assertion fails.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote in message
news:e6**************@TK2MSFTNGP02.phx.gbl...

>我昨晚在我编写的一些测试代码中遇到了这个bug。花了几分钟才弄明白根本原因是什么,让我想到了,哇。这是一个有趣的,并不经常出现!


所以,为了好玩,谁看到了这个bug并且可以解释它为什么会发生?


List< Threadthreads = new List< Thread>();

for(int i = 0; i< 2; i ++)

{

线程t =新线程(委托()

{

Debug.Assert(i!= 2);

});


t.Start();

threads.Add(t);

}


foreach(线程中的线程)

thread.Join();


-

Chris Mullins
>I hit this bug last night in some test code I had written. It took a few
minutes to figure out what the root cause was, and it left me thinking,
"Wow. That was an interesting one that doesn''t come up very often!".

So, for fun, who sees the bug and can explain why it''s happening?

List<Threadthreads = new List<Thread>();
for (int i = 0; i < 2; i++)
{
Thread t = new Thread(delegate()
{
Debug.Assert(i != 2);
});

t.Start();
threads.Add(t);
}

foreach (Thread thread in threads)
thread.Join();

--
Chris Mullins



10月24日下午2:19,Chris Mullins [MVP - C#]" ; < cmull ... @ yahoo.com>

写道:
On Oct 24, 2:19 pm, "Chris Mullins [MVP - C#]" <cmull...@yahoo.com>
wrote:

我昨晚在我写的一些测试代码中遇到了这个bug 。花了几分钟b $ b分钟来弄清楚根本原因是什么,它让我思考,

哇。这是一个有趣的,并不经常出现!


所以,为了好玩,谁看到了这个bug并且可以解释它为什么会发生?


List< Threadthreads = new List< Thread>();

for(int i = 0; i< 2; i ++)

{

线程t =新线程(委托()

{

Debug.Assert(i!= 2);

});


t.Start();

threads.Add(t);


}


foreach(线程中的线程)

thread.Join();


-

Chris Mullins
I hit this bug last night in some test code I had written. It took a few
minutes to figure out what the root cause was, and it left me thinking,
"Wow. That was an interesting one that doesn''t come up very often!".

So, for fun, who sees the bug and can explain why it''s happening?

List<Threadthreads = new List<Thread>();
for (int i = 0; i < 2; i++)
{
Thread t = new Thread(delegate()
{
Debug.Assert(i != 2);
});

t.Start();
threads.Add(t);

}

foreach (Thread thread in threads)
thread.Join();

--
Chris Mullins



你好Chris,

看起来你把我增加到2在线程开始之前。

你可以看到同一个线程没有匿名委托:


private static int _global;

public static void Main()

{

_global = 1;

线程t =新线程(MyThreadProc);

t.Start();


_global = 2;

t.Join();

}


public static void MyThreadProc()

{

Debug.Assert(_global!= 2);

}

Hi Chris,
It looks like you''re incrementing i to 2 before the threads start.
You can see the same thing with one thread and no anonymous delegate:

private static int _global;
public static void Main()
{
_global = 1;
Thread t = new Thread(MyThreadProc);
t.Start();

_global = 2;
t.Join();
}

public static void MyThreadProc()
{
Debug.Assert(_global != 2);
}


这篇关于发现Bug:Fun Concurrency Bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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