多线程文章最终“完成”。 - 评论者欢迎 [英] Multi-threading article finally "finished" - reviewers welcome

查看:69
本文介绍了多线程文章最终“完成”。 - 评论者欢迎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅交叉帖子 - 我很确定我对所有发布的群组的

文章感兴趣。


我终于设法完成了关于多线程的文章 - 至少

。如果对

多线程感兴趣的人会阅读它(即使只是它的一部分 - 它有点

长,我会非常感激通过整个事情!)检查准确性,

例子的有效性等。


随时直接发邮件给我发表评论或发布这里。


文章位于:
http://www.pobox.com/~skeet/csharp/multithreading.html


-

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

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

Please excuse the cross-post - I''m pretty sure I''ve had interest in the
article on all the groups this is posted to.

I''ve finally managed to finish my article on multi-threading - at least
for the moment. I''d be *very* grateful if people with any interest in
multi-threading would read it (even just bits of it - it''s somewhat
long to go through the whole thing!) to check for accuracy,
effectiveness of examples, etc.

Feel free to mail me directly with comments or post them here.

The article is at:
http://www.pobox.com/~skeet/csharp/multithreading.html

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

推荐答案

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

我终于设法完成了关于多线程的文章 - 至少目前是
。如果对多线程有兴趣的人会阅读它(即使只是它的一部分 - 它有点长,可以完成整个事情,我会非常感激!)检查准确性,示例的有效性等。

随时直接给我发送评论或发布在这里。

I''ve finally managed to finish my article on multi-threading - at least
for the moment. I''d be *very* grateful if people with any interest in
multi-threading would read it (even just bits of it - it''s somewhat
long to go through the whole thing!) to check for accuracy,
effectiveness of examples, etc.

Feel free to mail me directly with comments or post them here.




我浏览了你的文章,看起来写得很好。我对你可能感兴趣的几个部分有一些

的一些反馈:


关于volitile和stale数据的部分,我虽然这个例子对于初学者来说,文字是好的,但是没有触及那是什么(是!)最大的问题

对我来说 - 这就是问题。它在调试模式下运行良好,但是当我打开优化时,我的代码就不再起作用了。问题。

各种优化器可以并且确实在循环内部隔离变量以及其他非常微妙的东西的方式让我惊讶不止一次。你的b
建议把东西锁在一个锁中以避免''volitile''

问题非常好 - 特别是因为它可以在VB.NET中工作,缺少

volitile关键字。


停止中的代码示例部分,我不同意。首先

Nitpicks:

您正在使用变量停止和停止,以及属性停止

和已停止。我相信这个命名惯例是不赞成的。

依靠案例区分成员和财产只是要求

麻烦。它也违反了MS在MSDN中为.NET命名的命名约定。


我通常将此代码编写为:


Private _stop As New Threading.ManualResetEvent(False)

Private _runningThread As Threading.Thread


Public Sub StartTheThread()

_stop.Reset()

_runningThread = New Threading.Thread(AddressOf MyThread)

_runningThread.Start()

结束Sub


Public Sub StopTheThread()

_stop.Set()

_runningThread.Join()

结束子


私人子MyThread()

虽然不是_stop.WaitOne(0,False)

''做在这里工作

结束时

结束次级


由于某些原因,我更喜欢你的样本

1 - 这是我的信念(我没有对它进行基准测试,虽然我应该),测试Set / Not-Set事件的价格要比购买和/或$ b $便宜得多。 b释放我onitor(可能)每秒数百万次。


2 - 如果我的线程循环不必尽可能快地运行,我可以轻松地

更改为While Not _stop.WaitOne(1000,False)让我的循环每秒运行一次
,同时响应关机事件。

在你的示例代码中,我需要使用Sleep来减慢在循环中,

使线程无法响应关闭。


3 - 您的代码实际上是创建手动重置事件。为什么要创建

已经存在的东西?

我希望在那里看到几个部分 - 大多数是

这些是我已经有一段时间没有的问题,而且从来没有能够获得优秀的激光器......


1 - 我已经找到了一些关于信息的东西,并且绝对没有找到任何东西,就是同步的东西。各种集合类的版本。在很多情况下,
最终不得不滚动我自己的收藏品,因为我需要从多个线程中获取
(我很期待

泛型!)。由于缺少同步集合

课程的文档,我从不习惯使用它们。那么它们是线程安全的吗?

我很确定Iteration不是(虽然它可能是 - 迭代器可以

抓住一个锁,并阻止其他迭代器来自继续,直到它完成了b $ b。哈希表说(某处)一个作家和多个读者

是线程安全的,但我从来没有这么肯定。那个其他的

系列怎么样?


2 - 人们一直用于

的一些Win32线程结构如此之久在.NET中不存在。特别是信号量和(我认为)命名

事件。知道为什么吗?


3 - 关于如何开始调试线程代码的一些建议。这些可能是简单的建议,例如给你所有的线程都有唯一的名字,这样那么

你在看调试器中的Threads窗口,你可以快速告诉

哪个帖子是或复杂的建议,比如使用SoS确定

哪个帖子持有一个锁。


4 - 您没有关于事件的部分(手动或自动重置)。与监视器一起使用
,这些是最常用的线程结构。


5 - ReaderWriterLocks有时很方便,但可能超出范围

你的文章。


6 - 另一个我没见过的优点话题,但似乎好像它可以真的是b $ b有用的,是Threadpool绑定一个句柄

的能力,并在此句柄发生变化时提供回调。我可以将

标准等待句柄绑定到线程池,以便每当

手动或自动重置甚至更改时我都可以回调吗?似乎可能会有一些非常有用的用例......


-

Chris Mullins



I scanned through your article and it seemed very well written. I have a
little bit of feedback on a few sections that you may be interested in:

The section on volitile and stale data, I though the example and text was
good for beginners, but didn''t touch on what was (is!) the biggest issue
there for me - that is the problem of "it runs great in debug mode, but when
I turn on optimizations my code doesn''t work any more." issue. The way the
various optimizers can and do isolate variables inside loops and other very
subtle things has caught me by surprise on more than one occasion. Your
suggestion of wrapping things in a lock in order to avoid the ''volitile''
issue is excellent - especially as it''ll work in VB.NET, which lacks the
volitile keyword.

The code samples in the "Stopping" section, I don''t agree with. First the
Nitpicks:
You are using variables "stopping" and "stopped", and properties "Stopping"
and "Stopped". This naming convention shoud, I believe, be frowned upon.
Relying on case to differentiate members and properties is just asking for
trouble. It also violates the naming convention MS spells out in the MSDN
documentation for .NET.

I typically write this code as:

Private _stop As New Threading.ManualResetEvent(False)
Private _runningThread As Threading.Thread

Public Sub StartTheThread()
_stop.Reset()
_runningThread = New Threading.Thread(AddressOf MyThread)
_runningThread.Start()
End Sub

Public Sub StopTheThread()
_stop.Set()
_runningThread.Join()
End Sub

Private Sub MyThread()
While Not _stop.WaitOne(0, False)
'' Do Work Here
End While
End Sub

I prefer this over your sample for a few reasons
1 - It is my belief (I have not benchmarked it, although I should) that
testing an event for Set/Not-Set is much cheaper than acquiring and
releasing a monitor (potentially) several millions times per second.

2 - If my thread loop doesn''t have to run "as fast as it can", I can easilyl
change the to "While Not _stop.WaitOne(1000, False)" to make my loop run
once per second, while at the same time being responsive to shutdown events.
In your sample code, I would need to use a Sleep to slow down the loop,
which makes the thread unresponsive to shutting down.

3 - Your code is, in essece, creating a Manual Reset Event. Why create
something that''s already there?
There were a few sections that I would have liked to see in there - mostly
these are questions that I''ve had for a while, and haven''t ever been able to
get good ansers to..

1 - Something that I''ve looked for information on, and found absolutly
nothing, is the "Synchronized" version of the various collection classes. I
have ended up having to roll my own collections in many cases, as I need to
access them from multiple threads (I am sooooo looking forward to
Generics!). With the lack of documentation on the synchronized collection
classes, I was never comfortable using them. What about them is thread safe?
I''m pretty sure Iteration is not (although it could be - the iterator could
grab a lock, and prevent other iterators from proceeding until it''s
complete). Hashtables say (somewhere) that "one writer and multiple readers"
is thread safe, but I''ve never been quite sure of that. What about the other
collections?

2 - Some of the Win32 threading constructs that people have been using for
so long are not present in .NET. Specifically Semaphores and (I think) Named
Events. Any idea why?

3 - Some suggestions on how to start debugging threaded code. These could be
simple suggestions like "Give all your threads unique names, so that then
you''re looking at the Threads window in the debugger, you can quickly tell
which thread is which", or complex suggestions like "Use SoS to determine
which thread is holding a lock".

4 - You don''t have a section on Events (either Manual or AutoReset). Along
with Monitors, these are the most used threading constructs.

5 - ReaderWriterLocks are handy at times, but are probably beyond the scope
of your article.

6 - Another advanded topic that I''ve seen little on, but seems as if it
could be really usefull, is the ability of the Threadpool to have a Handle
bound to it, and provide callbacks when this handle changed. Can I bind
standard wait handles to the thread pool so that I can callbacks whenever a
manual or auto-reset even is changed? It seems as if there would probably be
some fascinating use cases for this...

--
Chris Mullins


非常好的文章;快速浏览一下,这是一个简单的解释方法

不那么容易的问题:)


回到VB时代,每个人都想要多个线程(甚至用api

*高度不稳定*),但现在我没有看到人们在.NET上使用它,因为它看起来很自然。


我自己在.NET上的多线程应用程序上花费了一半时间

我了解到如果没有某种互斥体它会变得多么混乱。
Very nice article; had a quick look through it, its an easy way to explain
not-so-easy issues :)

Back in the VB days everybody wanted multi-threads (even did it with api
*highly unstable*) but now I don′t see people using it as much on .NET as it
would seem natural to.

I myself had a time and half on a multi-threading application on .NET where
I learned just how messy can it get without some sort of mutex.


一个很好的补充可能是对ManualResetEvent和AutoResetEvent

的讨论,特别是它们之间的差异以及它们是否合适......我

一直有问题。


Greg


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
A good addition might be a discussion of ManualResetEvent and AutoResetEvent
especially the differences between them and when they are appropriate ... I
get questions on that all the time.

Greg

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
请原谅交叉帖子 - 我很确定我对所有发布的群组的
文章感兴趣。

我我终于设法完成了关于多线程的文章 - 至少目前是这样。如果对多线程有兴趣的人会阅读它(即使只是它的一部分 - 它有点长,可以完成整个事情,我会非常感激!)检查准确性,示例的有效性等。

随时直接给我发送评论或发布在这里。

文章在:
http://www.pobox.com/~skeet /csharp/multithreading.html

- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要也寄给我
Please excuse the cross-post - I''m pretty sure I''ve had interest in the
article on all the groups this is posted to.

I''ve finally managed to finish my article on multi-threading - at least
for the moment. I''d be *very* grateful if people with any interest in
multi-threading would read it (even just bits of it - it''s somewhat
long to go through the whole thing!) to check for accuracy,
effectiveness of examples, etc.

Feel free to mail me directly with comments or post them here.

The article is at:
http://www.pobox.com/~skeet/csharp/multithreading.html

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



这篇关于多线程文章最终“完成”。 - 评论者欢迎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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