When I was testing the use of locks in multi-threaded situations, I found that the locks were not working properly, and many locks were entered and released simultaneously.
var etcdClient = new EtcdClient("http://127.0.0.1:2379"); for (var i = 0; i < 10; i++) { var k = i; Task.Run(() => { var date = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss:fff"); var x = etcdClient.Lock("key-20231229", deadline: DateTime.UtcNow.AddMinutes(2)); Console.WriteLine($"take lock:{k},{date}"); Thread.Sleep(Random.Shared.Next(1000, 5000)); //Thread.Sleep(8000); etcdClient.Unlock("key-20231229"); Console.WriteLine($"release lock:{k},{date}"); }); }
take lock:9,2023/12/29 04:16:02:396 release lock:9,2023/12/29 04:16:02:396 take lock:1,2023/12/29 04:16:02:396 take lock:3,2023/12/29 04:16:02:396 take lock:8,2023/12/29 04:16:02:396 take lock:7,2023/12/29 04:16:02:396 take lock:4,2023/12/29 04:16:02:396 take lock:0,2023/12/29 04:16:02:396 take lock:5,2023/12/29 04:16:02:396 take lock:2,2023/12/29 04:16:02:396 take lock:6,2023/12/29 04:16:02:396 release lock:2,2023/12/29 04:16:02:396 release lock:0,2023/12/29 04:16:02:396 release lock:5,2023/12/29 04:16:02:396 release lock:1,2023/12/29 04:16:02:396 release lock:4,2023/12/29 04:16:02:396 release lock:8,2023/12/29 04:16:02:396 release lock:3,2023/12/29 04:16:02:396 release lock:6,2023/12/29 04:16:02:396 release lock:7,2023/12/29 04:16:02:396
.net version : .net 6 dotnet-etcd version : 6.0.1
When I was testing the use of locks in multi-threaded situations, I found that the locks were not working properly, and many locks were entered and released simultaneously.
var etcdClient = new EtcdClient("http://127.0.0.1:2379"); for (var i = 0; i < 10; i++) { var k = i; Task.Run(() => { var date = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss:fff"); var x = etcdClient.Lock("key-20231229", deadline: DateTime.UtcNow.AddMinutes(2)); Console.WriteLine($"take lock:{k},{date}"); Thread.Sleep(Random.Shared.Next(1000, 5000)); //Thread.Sleep(8000); etcdClient.Unlock("key-20231229"); Console.WriteLine($"release lock:{k},{date}"); }); }
take lock:9,2023/12/29 04:16:02:396 release lock:9,2023/12/29 04:16:02:396 take lock:1,2023/12/29 04:16:02:396 take lock:3,2023/12/29 04:16:02:396 take lock:8,2023/12/29 04:16:02:396 take lock:7,2023/12/29 04:16:02:396 take lock:4,2023/12/29 04:16:02:396 take lock:0,2023/12/29 04:16:02:396 take lock:5,2023/12/29 04:16:02:396 take lock:2,2023/12/29 04:16:02:396 take lock:6,2023/12/29 04:16:02:396 release lock:2,2023/12/29 04:16:02:396 release lock:0,2023/12/29 04:16:02:396 release lock:5,2023/12/29 04:16:02:396 release lock:1,2023/12/29 04:16:02:396 release lock:4,2023/12/29 04:16:02:396 release lock:8,2023/12/29 04:16:02:396 release lock:3,2023/12/29 04:16:02:396 release lock:6,2023/12/29 04:16:02:396 release lock:7,2023/12/29 04:16:02:396