Multi-thread usage summary and multi-thread Summary

Source: Internet
Author: User

Multi-thread usage summary and multi-thread Summary

Threads are generally used in computing-intensive operations or want to achieve asynchronous results.

Creating and destroying threads all consume resources. Therefore, the more threads, the better. C # provides the thread pool object. The advantage of the thread pool is that the thread can be reused and the number of concurrent threads is automatically controlled. Microsoft does not recommend that you modify the maximum number of concurrent threads.

Threads are divided into Front-End threads and backend threads. The difference is that the application must run all the front-end threads before exiting. For background threads, the application can exit directly regardless of whether it has been run. All background threads automatically end when the application exits. C # The created Thread is a front-end Thread by default. You can use the Thread class IsBackground to set whether it is a background Thread (which must be set before the Start method ).

A Task is actually a thread in a thread pool, which is easier to use than a thread pool. The threads used in Parallel may be in the thread pool or not.

Thread Synchronization:

For simple numeric operations, InterLocked lightweight lock objects should be used. The ReaderWriterLockSlim object should be used for file stream operations.

In other cases, you can use Monitor, Mutex (Mutex lock), or Semaphore.

Lock (obj) is equivalent to using the Monitor object. The obj object is a critical section, which is generally a (new object). It cannot be a value-type object, a string (or a singleton object), or a this object. When the value type is used, the packing operation is performed, so that each generated critical section is different and does not have a mutex effect. String type because of the memory resident technology, all "abc" is actually the same object, so other places that use "abc" may have to wait for a long time (the same is true for Singleton objects ). This object may cause deadlocks if it is locked.

Mutex is more important and is generally used for inter-process synchronization.

The advantage of Semaphore is that it can control the concurrency.

You can also use semaphores for complex synchronization control. It is worth noting that the difference between ManualResetEvent and AutoResetEvent is that after ManualResetEvent calls the Set method, all the waiting threads continue to execute, the AutoResetEvent will be executed by a thread immediately after the Set method is called.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.