zoukankan      html  css  js  c++  java
  • C# 等值锁定

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace FZJ.Lock
    {
        /// <summary>
        /// 等值锁定
        /// 锁定给定内容
        /// 内容必须实现IComparable接口,若Comparable比较两个对象是同一个值,
        /// 则在相等对象上执行的操作是同步的
        /// 实际上未对给定对象进行锁定,在外部可以调用lock等
        /// </summary>
        public class EquivalentLock<T> where T : IComparable<T>
        {
            Dictionary<T, Semaphore> t2Event = new Dictionary<T, Semaphore>();
    
            System.Threading.SpinLock sl = new SpinLock();
            /// <summary>
            /// 进入锁定内容
            /// </summary>
            /// <param name="lockObj">被锁定的内容</param>
            public void Enter(T lockObj)
            {
                bool lockTaken = false;
                sl.Enter(ref lockTaken);
                if (!t2Event.ContainsKey(lockObj))
                {
                    t2Event.Add(lockObj, new Semaphore(1, 1));
                }
                sl.Exit();
                t2Event[lockObj].WaitOne();
            }
            /// <summary>
            /// 退出锁定内容
            /// </summary>
            /// <param name="lockObj">锁定的内容</param>
            public void Exit(T lockObj)
            {
                if (!t2Event.ContainsKey(lockObj))
                {
                    throw new KeyNotFoundException();
                }
                t2Event[lockObj].Release();
            }
            /// <summary>
            /// 释放存储资源
            /// </summary>
            public void Free()
            {
                t2Event.Clear();
            }
        }
    }


    方法调用方式。

        class Program
        {
            static void Main(string[] args)
            {
    
                EquivalentLock<string> stringLock = new EquivalentLock<string>();
    
                for (int i = 0; i < 10; i++)
                {
                    new Thread(() =>
                    {
                        string ss = new string('1', 3);
    
                        int threadHashCode = System.Threading.Thread.CurrentThread.GetHashCode();
                        Console.WriteLine(threadHashCode + "尝试进来");
                        stringLock.Enter(ss);
                        //do something
                        Console.WriteLine(threadHashCode +  "进来");
                        Thread.Sleep(1000);
                        Console.WriteLine(threadHashCode + "出来");
                        stringLock.Exit(ss);
                        
                    }).Start();
                }
    
    
                Console.WriteLine("是否释放资源?(y/n)");
                string isFree = Console.ReadLine();
                if (isFree == "y")
                {
                    stringLock.Free();
                }
                Console.Read();
            }
    }
  • 相关阅读:
    Usb key插入检测,并动态获取CSP
    PsGetVersion
    BCDEDIT 启动配置数据存储编辑器
    fatal error LNK1103: debugging information corrupt; recompile module 解决
    Smart Card(windows)
    findwindow 后postmessage点解按钮
    核心层API分类
    64位下SetWindowLong时的参数GWL_WNDPROC undeclared identifier未定义的错误
    安装SQL SERVER 2008时出现了SQL SERVER 2005 Express Tool Installed 的错误
    win7重置密码的方法
  • 原文地址:https://www.cnblogs.com/LittleJin/p/4876681.html
Copyright © 2011-2022 走看看