C # multithreading mechanism (3)

Source: Internet
Author: User

C # multithreading mechanism (3)

Note that other threads are attached to the thread where the Main () function is located. The Main () function is the entry of the C # program. The starting thread can be called the Main thread, if all foreground threads are stopped, the main thread can be terminated, and all background threads will be terminated unconditionally. While all threads are executed in a serial way at the micro level, you can think of them as being executed in parallel at the macro level.

The reader must have noticed the Thread. threadState, which represents the state of a thread during runtime and has different values under different circumstances. Therefore, we can design a program flow by judging the value. ThreadState may take the following values in various situations:

Aborted: the thread has stopped.
AbortRequested: The Thread. Abort () method of the Thread has been called, but the Thread has not stopped.
Background: The Thread is executed in the Background, which is related to the attribute Thread. IsBackground.
Running: The thread is Running normally.
Stopped: the thread has been Stopped.
StopRequested: The thread is being requested to stop
Suincluded: the thread has been Suspended. (In this status, you can call Resume () to run the thread again)
SuspendRequested: The thread is requesting to be suspended, but cannot respond.
Unstarted: Thread. Start () is not called to Start the Thread.
WaitSleepJoin: The thread is blocked because it calls methods such as Wait (), Sleep (), and Join ().

As mentioned above, the Background State indicates that the thread is running in the Background. What are the special features of the backend running threads? In fact, there is only one difference between the background thread and the foreground thread, that is, the background thread does not prevent program termination. Once all foreground threads of a process are terminated, CLR (general language runtime environment) will completely terminate the process by calling the Abort () method of any surviving background process.

When the threads compete for the CPU time, the CPU is given the service according to the priority of the thread. In the C # application, you can set five different priorities, from high to low, which are Highest, AboveNormal, Normal, BelowNormal, and Lowest. If the priority is not specified during thread creation, the default value is ThreadPriority. normal. Specify a priority for a thread
You can use the following code:

// Set the priority to the lowest
MyThread. Priority = ThreadPriority. Lowest;

By setting the thread priority, we can arrange some important threads for priority execution, such as user response.

Now we have a preliminary understanding of how to create and control a thread. Next we will study the typical problems in thread implementation in depth and discuss the solution.

3. Synchronization and communication of threads-producer and consumer

Assume that two threads maintain a queue at the same time. If one thread adds an element to the queue and the other thread uses the element from the queue, we call the thread for adding elements as the producer, and the thread for using elements as the consumer. The producer and consumer problems seem simple, but they are a problem that must be solved in multi-threaded applications. They involve synchronization and communication between threads.

As mentioned above, each thread has its own resources, but the code zone is shared, that is, each thread can execute the same function. However, in a multi-threaded environment, the possible problem is that several threads execute a function at the same time, resulting in data confusion and unexpected results. Therefore, we must avoid this situation. C # provides a keyword lock, which defines a piece of code as a critical section. A mutex section allows only one thread to enter the execution at a time point, other threads must wait. In C #, the keyword lock is defined as follows:

Lock (expression) statement_block

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.