Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Threading [closed]

I'm learning Multi Threading at the moment, in C#, but as with all learning I like to learn best practices. At the moment the area seems fuzzy. I understand the basics, and I can create threads.

What should I look out for when creating multi threaded applications. Are there any set rules or best practices that I should know about? Or anything to remember in order to avoid slip ups down the line?

Thanks for the responses.

like image 995
James Jeffery Avatar asked Jan 28 '10 18:01

James Jeffery


People also ask

What is multithreading?

Introduction to Multithreading Multithreading is the phenomenon of executing more than a thread in the system, where the execution of these threads can be of two different types, such as Concurrent and Parallel multithread executions.

What is the difference between a thread and multiprocessing?

A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. However, we use multithreading than multiprocessing because threads use a shared memory area.

What is the many to one model of threading?

Since a lot of kernel threads burden the system, there is restriction on the number of threads in the system. The many to one model maps many of the user threads to a single kernel thread. This model is quite efficient as the user space manages the thread management.

What are the applications of multiple threads in programming?

Applications which involve mechanism like validate and save, produce and consume, read and validate are done in multiple threads. Few examples of such applications are online banking, recharges, etc. It can be used to make games where different elements are running on different threads.


2 Answers

In addition to the MSDN Best Practices, I'll add:

  1. Don't make your own threads. Prefer to use the ThreadPool (or the new Task Parallel Library Tasks). Managing your own thread is rarely, if ever, the correct design decision.
  2. Take extra care with UI related issues. Control.Invoke (Windows Forms) and Dispatcher.Invoke (WPF), or use SynchronizationContext.Current with Post/Send
  3. Favor using the BackgroundWorker class when appropriate.
  4. Try to keep synchronization via locks to a minimum
  5. Make sure to synchronize everything that requires synchronization
  6. Favor the methods in the Interlocked class when possible over locking

Once you get more advanced, and are trying to optimize, other things to look for:

  1. Watch out for false sharing. This is especially problematic when working with arrays, since every array write to any element in an array includes a bounds check in .NET, which in effect causes an access on the array near element 0 (just prior to element 0 in memory). This can cause perf. to go downhill dramatically.
  2. Beware of closure issues, especially when working in looping situations. Nasty bugs can occur if you're closing on a variable in the wrong scope when making a delegate.
like image 89
Reed Copsey Avatar answered Oct 10 '22 19:10

Reed Copsey


MSDN - Managed Threading Best Practices

That MSDN Article does a really good job at touching on the danger areas and giving the best practices for managing/working around those areas.

like image 9
Justin Niessner Avatar answered Oct 10 '22 19:10

Justin Niessner