线程 - 问题 [英] Threading - issues

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

问题描述

嗨!


我已经使用线程完成了一些代码,只是为了检查编写代码是否不正确

在使用某种安全数据时可能会导致严重问题类型。


所以在我的理解中,堆栈(集合类型)不会给任何项目更多

而不是一次。但确实如此!!!


我再次不正确地编写了这段代码,看看会发生什么但是我不知道为什么会发生这种情况所以如果你理解的话问题只是回复;)


private void btnTestThreading_Click(object sender,EventArgs e)

{

Stack< intnumbersStack = new Stack< int>();

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

numbersStack.Push(i);


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

{

线程th =新线程(delegate()

{

//我把它作为参考,所以它应该是

很好地分享

TestThreading(ref numbersStack,i);

}

);


th.Start();

}

}


private void TestThreading(ref Stack< intnumbers,int threadNumber)

{

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

{

Thread.Sleep(numbers.Peek()* 10);

Console.WriteLin e(带号码的线程) + threadNumber +

" \t\t processing item" + numbers.Pop());

}

}

根据我的理解,最糟糕的情况是2线程

同时访问堆栈(我没有使用任何锁定)而且

相同的项目将被处理两次。但是发生的事情是1个线程两次获得相同项目的

...

如何可能?我会理解,如果它是其他线程,但它是

同一个。如果你删除Thread.Sleep它会停止(至少在我的;)


谢谢

Jedrzej

Hi!

I''ve done some code using threading just to check if writing code improperly
can cause serious issues when using somehow safe data types.

So in my understanding a stack ( collection type ) won''t give any item more
than once. But it does !!!

Again I wrote this code improperly to see what happens but I don''t
understand why it happens so if you understand the issue only than reply ;)

private void btnTestThreading_Click(object sender, EventArgs e)
{
Stack<intnumbersStack = new Stack<int>();
for (int i = 0; i < 100; i++)
numbersStack.Push(i);

for (int i = 0; i < 10; i++)
{
Thread th = new Thread(delegate()
{
// i''m putting this as a reference so it should be
shared nicely
TestThreading(ref numbersStack, i);
}
);

th.Start();
}
}

private void TestThreading(ref Stack<intnumbers, int threadNumber)
{
for (int i = 0; i < 10; i++)
{
Thread.Sleep(numbers.Peek() * 10);
Console.WriteLine("Thread with number " + threadNumber +
"\t\t processed item " + numbers.Pop());
}
}
In my understanding the worst case scenario here would be 2 threads
accessing stack in the same time ( i''m not using any locks ) and than the
same item would be ''processed'' twice. But what happens is 1 thread is getting
the same item twice...
HOW it''s possible ? I would understand if it was other thread but it''s the
same one. If you delete Thread.Sleep it stops ( at least at mine ;)

Thanks
Jedrzej

推荐答案

我怀疑你是捕获变量的受害者。 oni;

尝试以下内容:


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

{

int tmp = i; // **这是重要的

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

{

TestThreading(ref numbersStack,tmp); < br $>
}

);


th.Start();

}


我相信它们是不同的线程,但具有相同的标识;-p


我可以在一秒钟内解释(刚被某人访问)


Marc
I suspect you are victim of "captured variables" on "i";
try the following:

for (int i = 0; i < 10; i++)
{
int tmp = i; // **THIS IS IMPORTANT
Thread th = new Thread(delegate()
{
TestThreading(ref numbersStack, tmp);
}
);

th.Start();
}

I believe they were different threads, but with the same identifer ;-p

I can explain in a second (just got "visited" by someone)

Marc


我认为它们是不同的线程,但具有相同的标识;-p
I believe they were different threads, but with the same identifer ;-p

>
>



你是对的;)

你能解释一下这两个不同的线程是如何发生的吗?

在这种情况下,堆栈的结果会得到相同的结果。

当1个对象调用Pop()时,它应该从堆栈中删除一个项目,它们是访问内存中相同位置的
。 ..可能有一个愚蠢的解释

:)

我需要解释它如何工作以更好地理解:)


谢谢

Jedrzej

And you was right ;)
Could you explain how this happens that 2 diffrent threads accessing same
reference in this case stack are getting same results.
When 1 object invokes Pop() it should remove an item from the stack they are
accessing same place in the memory... There is probably a silly explanation
:)
I need an explanation of how it works to get better understanding :)

Thanks
Jedrzej


5月30日下午12:22,Jarod< Ja ... @ discussion.microsoft.comwrote:
On May 30, 12:22 pm, Jarod <Ja...@discussions.microsoft.comwrote:

我相信它们是不同的线程,但具有相同的标识符; -p
I believe they were different threads, but with the same identifer ;-p



你是对的;)

你能解释一下这种情况如何发生这种情况下,在这种情况下,两个不同的线程访问相同的

引用得到相同的结果。

当1个对象时调用Pop()它应该从堆栈中删除一个项目

访问内存中的相同位置......可能有一个愚蠢的解释

:)

我需要解释它是如何工作以更好地理解:)


And you was right ;)
Could you explain how this happens that 2 diffrent threads accessing same
reference in this case stack are getting same results.
When 1 object invokes Pop() it should remove an item from the stack they are
accessing same place in the memory... There is probably a silly explanation
:)
I need an explanation of how it works to get better understanding :)



那么,Stack声称是线程安全的吗?大多数.NET集合都没有b / b
声称 - 如果你从多个没有锁定的

线程访问相同的集合,我会期望看到一些奇怪的东西。当你在看

系列时获得锁定时,你会得到同样的问题吗?


另外,我想你对于什么引用有一些误解确实 -

请参阅 http:// pobox。 com / ~siget / csharp / parameters.html


Jon

Well, does Stack claim to be thread-safe? Most .NET collections don''t
make that claim - if you access the same collection from multiple
threads with no locking, I''d expect to see some oddness. Do you get
the same issues when you obtain a lock while looking at the
collection?

Also, I think you have some misunderstandings about what "ref" does -
see http://pobox.com/~skeet/csharp/parameters.html

Jon


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

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