扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
public void AcquireReaderLock( int millisecondsTimeout ) { // m_mutext很快可以得到,以便进入临界区 m_mutex.WaitOne( ); // 是否有写入线程存在 bool bExistingWriter = ( m_nActive < 0 ); if( bExistingWriter ) { //等待阅读线程数目加1,当有锁释放时,根据此数目来调度线程 m_nWaitingReaders++; } else { //当前活动线程加1 m_nActive++; } m_mutex.ReleaseMutex(); //存储锁标志为Reader System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot(m_strThreadSlotName); object obj = Thread.GetData( slot ); LockFlags flag = LockFlags.None; if( obj != null ) flag = (LockFlags)obj ; if( flag == LockFlags.None ) { Thread.SetData( slot, LockFlags.Reader ); } else { Thread.SetData( slot, (LockFlags)((int)flag | (int)LockFlags.Reader ) ); } if( bExistingWriter ) { //等待指定的时间 this.m_aeReaders.WaitOne( millisecondsTimeout, true ); } } |
public void AcquireWriterLock( int millisecondsTimeout ) { // m_mutext很快可以得到,以便进入临界区 m_mutex.WaitOne( ); // 是否有活动线程存在 bool bNoActive = m_nActive == 0; if( !bNoActive ) { m_nWaitingWriters++; } else { m_nActive--; } m_mutex.ReleaseMutex(); //存储线程锁标志 System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot( "myReaderWriterLockDataSlot" ); object obj = Thread.GetData( slot ); LockFlags flag = LockFlags.None; if( obj != null ) flag = (LockFlags)Thread.GetData( slot ); if( flag == LockFlags.None ) { Thread.SetData( slot, LockFlags.Writer ); } else { Thread.SetData( slot, (LockFlags)((int)flag | (int)LockFlags.Writer ) ); } //如果有活动线程,等待指定的时间 if( !bNoActive ) this.m_aeWriters.WaitOne( millisecondsTimeout, true ); } |
public void ReleaseReaderLock() { System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot(m_strThreadSlotName ); LockFlags flag = (LockFlags)Thread.GetData( slot ); if( flag == LockFlags.None ) { return; } bool bReader = true; switch( flag ) { case LockFlags.None: break; case LockFlags.Writer: bReader = false; break; } if( !bReader ) return; Thread.SetData( slot, LockFlags.None ); m_mutex.WaitOne(); AutoResetEvent autoresetevent = null; this.m_nActive --; if( this.m_nActive == 0 ) { if( this.m_nWaitingReaders > 0 ) { m_nActive ++ ; m_nWaitingReaders --; autoresetevent = this.m_aeReaders; } else if( this.m_nWaitingWriters > 0) { m_nWaitingWriters--; m_nActive --; autoresetevent = this.m_aeWriters ; } } m_mutex.ReleaseMutex(); if( autoresetevent != null ) autoresetevent.Set(); } |
using System; using System.Threading; using MyThreading; class Resource { myReaderWriterLock rwl = new myReaderWriterLock(); public void Read(Int32 threadNum) { rwl.AcquireReaderLock(Timeout.Infinite); try { Console.WriteLine("Start Resource reading (Thread={0})", threadNum); Thread.Sleep(250); Console.WriteLine("Stop Resource reading (Thread={0})", threadNum); } finally { rwl.ReleaseReaderLock(); } } public void Write(Int32 threadNum) { rwl.AcquireWriterLock(Timeout.Infinite); try { Console.WriteLine("Start Resource writing (Thread={0})", threadNum); Thread.Sleep(750); Console.WriteLine("Stop Resource writing (Thread={0})", threadNum); } finally { rwl.ReleaseWriterLock(); } } } class App { static Int32 numAsyncOps = 20; static AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false); static Resource res = new Resource(); public static void Main() { for (Int32 threadNum = 0; threadNum < 20; threadNum++) { ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum); } asyncOpsAreDone.WaitOne(); Console.WriteLine("All operations have completed."); Console.ReadLine(); } // The callback method's signature MUST match that of a System.Threading.TimerCallback // delegate (it takes an Object parameter and returns void) static void UpdateResource(Object state) { Int32 threadNum = (Int32) state; if ((threadNum % 2) != 0) res.Read(threadNum); else res.Write(threadNum); if (Interlocked.Decrement(ref numAsyncOps) == 0) asyncOpsAreDone.Set(); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者