A probe into the multithreading mechanism of C # (3)

Source: Internet
Author: User
Here we should note that the other threads are attached to the main () function of the thread, the main () function is the C # program's entry, the starting thread can be called the main thread, if all the foreground threads are stopped, then the main thread can be terminated, and all the background threads will be unconditionally terminated. While all threads are executed serially on a micro scale, you can think of them as parallel execution in the macro.

The reader must have noticed that the Thread.threadstate attribute, which represents the thread runtime state, has different values in different situations, so we can sometimes design the program flow by judging the value. The possible values of threadstate in various cases are as follows:

Aborted: Thread has stopped
AbortRequested: The thread's Thread.Abort () method has been called, but the thread has not stopped
Background: Thread is executed in the background, related to property thread.isbackground
Running: Thread is running correctly
Stopped: Thread has been stopped
Stoprequested: Thread is being requested to stop
Suspended: Thread has been suspended (in this state, it can be rerun by calling the Resume () method)
Suspendrequested: Thread is asking to be suspended but not responding
Unstarted: The run of the thread started without calling Thread.Start ()
WaitSleepJoin: Thread is blocked because it calls wait (), Sleep (), or join ()

The above mentioned that the background status indicates that the thread is running in the background, so what is the special place for the threads running in the background? In fact, the background thread has only one difference from the front thread, that is, the background thread does not prevent the program from terminating. Once all foreground threads of a process are terminated, the CLR (Common language Runtime) terminates the process completely by invoking the abort () method of any surviving background process.

When the threads compete for CPU time, the CPU is given the service as a thread priority. In C # applications, users can set 5 different priorities, from high to low, respectively, to Highest,abovenormal,normal,belownormal,lowest, and if you do not specify a priority when creating a thread, Then the system defaults to Threadpriority.normal. Assign a priority to a thread
, we can use the following code:

Set priority to lowest
Mythread.priority=threadpriority.lowest;

By setting the priority of the thread, we can schedule some relatively important threads to take precedence, such as the response to the user, and so on.

Now that we have a preliminary understanding of how to create and control a thread, we will delve into the typical problems of threading implementations and explore their solutions.

Three. Thread synchronization and communication--producers and consumers

Suppose that two threads maintain a queue at the same time, if one thread adds elements to the queue and another thread takes elements from the queue, we call the thread that adds the element as the producer, and the thread that takes the element as the consumer. The problem of producer and consumer seems simple, but it is a problem that must be solved in multi-threaded application, it involves the synchronization and communication problem between threads.

As mentioned earlier, each thread has its own resources, but the code area is shared, that is, each thread can execute the same function. But in a multithreaded environment, the problem is that several threads execute a function at the same time, causing the data to clutter and produce unpredictable results, so we must avoid this happening. C # provides a keyword lock that defines a piece of code as a mutex (critical section), where the mutex only allows one thread to execute at a time, while other threads must wait. In C #, the keyword lock is defined as follows:

Lock (expression) Statement_block

The above is the multithreading mechanism of C # (3) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.