Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

multithreading - Threading Best Practices

Many projects I work on have poor threading implementations and I am the sucker who has to track these down. Is there an accepted best way to handle threading. My code is always waiting for an event that never fires.

I'm kinda thinking like a design pattern or something.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(Assuming .NET; similar things would apply for other platforms.)

Well, there are lots of things to consider. I'd advise:

  • Immutability is great for multi-threading. Functional programming works well concurrently partly due to the emphasis on immutability.
  • Use locks when you access mutable shared data, both for reads and writes.
  • Don't try to go lock-free unless you really have to. Locks are expensive, but rarely the bottleneck.
  • Monitor.Wait should almost always be part of a condition loop, waiting for a condition to become true and waiting again if it's not.
  • Try to avoid holding locks for longer than you need to.
  • If you ever need to acquire two locks at once, document the ordering thoroughly and make sure you always use the same order.
  • Document the thread-safety of your types. Most types don't need to be thread-safe, they just need to not be thread hostile (i.e. "you can use them from multiple threads, but it's your responsibility to take out locks if you want to share them)
  • Don't access the UI (except in documented thread-safe ways) from a non-UI thread. In Windows Forms, use Control.Invoke/BeginInvoke

That's off the top of my head - I probably think of more if this is useful to you, but I'll stop there in case it's not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...