[C #] C # learning notes-Multithreading

Source: Internet
Author: User

C # learning notes-Multithreading

Luo chaohui (http://www.cnblogs.com/kesalin)

C # And. Net advancedProgramDesign Reading Notes

 

1, in. on the. NET platform, application domains and threads do not correspond one by one. An application domain can have multiple threads, A specific thread may not be limited to an application domain within its lifecycle. The Win32 thread scheduler and CLR allow the thread to freely span the boundaries of the application domain as needed, but at any specific time, a thread can only run in one application domain. The system. Threading namespace defines the thread-related types. Use thread. getdomain () when you want to access the application domain (which is carrying the current thread (). At any specific time, a thread can also be moved to a specific context, and a CLR can be redeployed to a new context. Use the thread. currentcontext attribute to obtain the current context of the thread being executed. The CLR control thread moves in/out of the application domain or context.

2. asynchronous delegation can be used to automatically create Asynchronous Method calls beyond the thread. The system. Threading namespace also defines many types for Synchronous access to shared resources.

3. The front-end thread can block the end of the application. The CLR can close the application (that is, uninstall the hosted application domain) until all foreground threads are terminated ). Background threads (sometimes called daemon threads) are considered by CLR as a way of sacrificing program execution, that is, they may be ignored at any time (even if the thread is executing a job at this time. Therefore, if all foreground threads terminate, the background threads are automatically terminated when the application domain is detached.

4. The preferred technology for Synchronous access to shared resources is the lock keyword of C, lock allows you to define the Code statement synchronized by a thread. It must define a tag (that is, an object reference ), the thread must obtain this mark when entering the lock range, and release the lock when exiting the lock range. When you try to lock an instance-level private method, you can use the reference of the object where the method itself is located. However, to lock a piece of code from a public member, it is safer to declare a private object member as the lock identifier. Example:

Public ClassPrinter
{
//Lock ID
Private ObjectThreadlock =New Object();
Public VoidPrintnumbers ()
{
//Use lock ID
Lock(Threadlock)
{
...
}
}

If you try to lock the code in the static method, you only need to declare a private static object member as the lock tag.

5. the C # Lock statement is actually a stenographer used with the system. Threading. moniter class. After being processed by the compiler, the lock area is actually converted into the following content (you can view it using ildasm.exe ):

Public VoidPrintnumbers ()
{
Monitor. Enter (threadlock );
Try
{
...
}
Finally
{
Monitor. Exit (threadlock );
}
}

6. system. Threading. Interlocked allows us to perform atomic operations on data: compareexchange (), decrement (), exchange (), and increment (). These static methods must be referenced to input variables. For example, note that the values of newval and intval are incremental values.

 
Public VoidAddone ()
{
IntNewval = interlocked. increment (RefIntval );
}

7. The [synchronization] feature can effectively ensure thread security for all instance members of the object. When the CLR assigns an object with the [synchronization] feature, it places the object in the synchronization context. This is a "lazy" Method for writing thread-safe code, because it does not require us to go deep into the thread to control details of sensitive data. However, this method has an image of performance, because even if a method does not use shared resources, the CLR will still lock the call to this method.

8. The threadpool is defined in the system. Threading namespace. The thread pool is maintained by CLR. The system. Threading namespace also contains the timer type and related timercallback delegation.

9. backgroundworker component: When a graphical windows form desktop application is built and tasks that take a long time outside the application UI thread need to be executed (such as calling a remote web service, ), The backgroundworker class is very useful. This type of programming model utilizes many identical thread syntaxes combined with asynchronous delegation. We only need to tell backgroundworker which method we want to execute in the background and call runworkerasync. The call thread continues to run, and the working method is asynchronously executed. when the execution of the working method is completed, the backgroundworker will notify the calling thread by triggering the runworkercompleted event.

 

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.