StackExchange/Code Reviewで見かけた質問とステキな回答。
質問:What are the best practices with multithreading in C#?
(Code ReviewはQ&Aサイトというより、名前の通りコードレビュー依頼が主目的)
回答:
Rather than reviewing your specific code -- which at first glance seems very, very buggy -- let me answer your question directly:
1. Don't. Multithreading is a bad idea. Programs are hard enough to understand already with a single point of control flow in them; why on earth would you want to add a second, third, fourth... ? Do you enjoy tracking down incredibly difficult-to-reproduce, impossible-to-understand bugs? Do you have an encyclopaedic knowledge of the differences between weak and strong memory models? No? Then don't write multithreaded programs! Processes are a perfectly good unit of work; if you have work to do, spawn a process.
2.(後略)
(参考訳)
あんたのそのコード ―ひと目見てバグだらけ― をレビューするより、質問に直接答えていこうか:
1. そんなものは無い。マルチスレッドは悪い考えだ。プログラム中に1つの制御フローがあるだけでも理解するのは十分難しいってのに;一体どうして2個目、3個目、4個目を増やそうとするんだ?途方も無く再現性が低くて、難解なバグを見つけだす作業を楽しみたい?あんたは弱いメモリモデルと強いメモリモデルの違いについて十分な知識があるか?ない?じゃあマルチスレッドプログラムを書くのなんて止めとけ!プロセスは申し分のない処理単位だ;そういうことをしたければ、プロセスを生成するんだね。
2.(後略)
回答では計8項目が挙げられています。興味があれば、ぜひ読んでみて:P