General Threading Issues

I'm kind of new to the thread in C # and had a couple of questions about what is there:

  • What are the ways to implement threads in C #? (i.e. I can think of two from above: backgroundWorker, Thread, etc.)

    • How do you cause a dead end, and if there is a dead end, how do you exit it (in C #)?

    • How does background work work? It seems to have a basic set of methods, but I would like to know that these methods and instances ...

Thanks!

+6
multithreading c #
source share
5 answers

The ultimate beginner's guide to the flow in C # is here: http://www.albahari.com/threading/

BackgroundWorker documentation with a full working example is here: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Dead ends are explained here: http://www.albahari.com/threading/part2.aspx

Themes can be implemented in many ways. You can use them directly, pull them out of ThreadPool, or use them indirectly using the Parallel Task Library .

+10
source share

What are the ways to implement threads in C #?

There are various ways to use streaming; some of which are associated with explicit thread creation, while others use already running threads.

How do you create a dead end, and if there is a dead end, how do you exit it (in C #)?

Here are three different ways to turn off a dead end. This list is not exhaustive.

Call the lock method from the lock section.

In this example, thread A gets a lock, and then immediately calls the lock method, while thread B tries to get the same lock, but gets a hang because thread A waits for thread B to signal an event before it will release the lock.

 public class Example { ManualResetEvent m_Event = new ManualResetEvent(false); void ThreadA() { lock (this) { m_Event.WaitOne(); } } void ThreadB() { lock (this) { m_Event.Set(); } } } 

Get two locks out of order.

No explanation is required here, as this is a well-known problem.

 public class Example { private object m_LockObjectA = new object(); private object m_LockObjectB = new Object(); void ThreadA() { lock (m_LockObjectA) lock (m_LockObjectB) { } } void ThreadB() { lock (m_LockObjectB) lock (m_LockObjectA) { } } } 

Lock lock.

This is one of my favorite illustrations of the dead end because the lock or block method is not involved. The subtleties of the problem are enough to confuse even those who are familiar with threads. The problem here is the lack of memory barriers. Thread A expects thread B to set the signal flag, while thread B waits for thread A to reset, all this time, none of the threads see the changes that the other makes, because the compiler, JIT and hardware are free to optimize reading and writing a flag in a way that is not intuitive.

 public class Example { private bool m_Signal = false; void ThreadA() { while (!m_Signal); m_Signal = false; } void ThreadB() { m_Signal = true; while (m_Signal); } } 

How does a background worker work implemented?

Here is a very simple step-by-step procedure for you to get started.

  • Add an event handler that does the actual work with the DoWork event.
  • Add an event handler to get progress information in the ProgressChanged event.
  • Add an event handler that will be executed after the RunWorkerCompleted event is RunWorkerCompleted .
  • Call RunWorkerAsync from the user interface thread to start the background. This raises the DoWork event in a separate thread.
  • Call ReportProgress periodically from the DoWork event DoWork to publish new progress information. This raises the ProgressChanged event in the user interface thread.
+9
source share

.net 4 offers parallel LINQ. This is very nice if you want to parallelize a free effect without side effects, which is easily expressed in a functional / linq-style.

0
source share

For all common goals and goals, use Thread . If you want to talk from some thread to the GUI, you might consider using BackgroundWorker , because it automatically serializes (with Invoke() ) calls to GUI methods, so you won't have a problem locking the GUI.

And like dead ends, do not worry about them. Deadlocks are possible only if you have 2 threads competing for the same set of resources, and I think you canโ€™t handle it yet.

0
source share

I would classify the answer in 3 sections. Thus, with .net 4.0, all the above examples fall into 3 main categories: 1. Themes managed by the .net thread pool (asynchronous delegate call, background worker, etc.) 2. Stream class - you need to manage the lifetime yourself stream and finally Parallel Linq, which requires a multi-core processor.

0
source share

All Articles