Exemplo n.º 1
0
        static void Test()
        {
            MutilThreadReadWriterLock x = new MutilThreadReadWriterLock();

            TimeSpan sp = new TimeSpan(0, 0, 1, 0);

            Console.WriteLine("Thread ID: {0}", id);

            Interlocked.Increment(ref id);

            while (true)
            {
                using (IDisposeState y = x.LockRead(sp))
                {
                    if (y.IsValid)
                    {
                        Console.WriteLine("Number: {0}", t);
                    }
                }

                using (IDisposeState y = x.LockWrite(sp))
                {
                    if (y.IsValid)
                    {
                        t++;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public IDisposeState LockWrite(TimeSpan timeout, Func <bool> isvalidstate)
        {
            IDisposeState state = null;
            Func <bool>   f     = () =>
            {
                if (dispose)
                {
                    state = DisposeState.Empty;
                    return(true);
                }
                if (TryLockWrite())
                {
                    bool isvalid = isvalidstate != null?isvalidstate() : true;

                    if (isvalid)
                    {
                        state = new MutiThreadDisposeState(isvalid, true, this);
                    }
                    else
                    {
                        state = DisposeState.Empty;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            };

            if (rwLock.WaitForTime(timeout, f))
            {
                return(state);
            }
            else
            {
                return(DisposeState.Empty);
            }
        }