In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "C# multithreaded deadlock example code analysis". In daily operation, I believe that many people have doubts about C# multithreaded deadlock instance code analysis. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "C# multithreaded deadlock example code analysis". Next, please follow the editor to study!
Introduction to deadlock
In a multiprogramming environment, multiple processes may compete for a certain number of resources. A process requests a resource, and if the resource is not available, the process enters a waiting state. If the requested resource is occupied by another waiting process, the waiting process may not be able to change the state, which is called a deadlock.
2. Deadlock conditions
Four conditions for deadlock:
1. Non-preemptive: resources cannot be preempted
two。 Mutex: at least one resource must be in non-shared mode, that is, only one process can use it at a time, and if another process requests the resource, the application process must be delayed until the resource is released.
3. Take and wait: a process must occupy at least one resource and wait for another resource, which is owned by another process.
4. Loop wait: there is a set of processes {P0pm P1, … Pn}, the resources waiting for P0 are occupied by P1, the resources waiting for P1 are occupied by P2, the resources waiting for Pn-1 are occupied by Pn, and the resources waiting for Pn are occupied by P0.
These four conditions must be met to form a deadlock. Then violating any of these conditions will not form a deadlock, which becomes deadlock prevention, and deadlock avoidance dynamically detects whether the state of allocating resources is secure.
III. Solutions
1. We can deal with it by adding timeliness to the lock, or by deadlock detection.
The 2.Monitor.TryEnter () method. Although this approach can solve the deadlock problem, it is best not to have a deadlock.
Deadlock case code
The following code implementation creates two threads T1 and T2 that initially allocate lock1 and ock2 resources, respectively. When one thread T1 occupies the resource lock1 and then requests another resource lock2, the request fails and waits for the release of resources because another thread T2 is occupying the resource T2. At the same time, T2 sends out a request for resource lock1, but lock1 is occupied by T1, so it is not released and can only wait. At this point, two threads can only wait for each other to release resources, which is called a deadlock.
Namespace Deadlock {class DL {int field1 = 0; int field2 = 0; private object lock1 = new int [1]; private object lock2 = new int [1] Public void First (int val) {lock (lock1) {Console.WriteLine ("First: Acquired lock1:" + Thread.CurrentThread.GetHashCode () + "Now Sleeping."); / / Try commenting Thread.Sleep () Thread.Sleep (1000) / / at this time, the T2 thread occupies the resource Console.WriteLine ("First: Acquired lock 1:" + Thread.CurrentThread.GetHashCode () + "Now wants lock2.") / / when a lock lock2 is requested, lock is occupied by the T2 thread, waiting for the release of lock (lock2) {Console.WriteLine ("First: Acquired lock2:" + Thread.CurrentThread.GetHashCode ()); field1 = val; field2 = val } public void Second (int val) {lock (lock2) {Console.WriteLine ("Second: Acquired lock2:" + Thread.CurrentThread.GetHashCode ()) / / when a lock lock1 is requested, the lock1 is occupied by the T1 thread, waiting to release lock (lock1) {Console.WriteLine ("Second: Acquired lock1:" + Thread.CurrentThread.GetHashCode ()); field1 = val; field2 = val }} public class MainApp {DL d = new DL (); public static void Main () {MainApp m = new MainApp (); Thread T1 = new Thread (new ThreadStart (m.Run1)); t1.Start (); Thread T2 = new Thread (new ThreadStart (m.Run2)) T2.Start (); Console.ReadLine ();} public void Run1 () {this.d.First (10);} public void Run2 () {this.d.Second (10) At this point, the study on "C# multithreaded deadlock instance code analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.