當前位置: 首頁>>代碼示例>>C#>>正文


C# Array.ReleaseWriteLock方法代碼示例

本文整理匯總了C#中System.Array.ReleaseWriteLock方法的典型用法代碼示例。如果您正苦於以下問題:C# Array.ReleaseWriteLock方法的具體用法?C# Array.ReleaseWriteLock怎麽用?C# Array.ReleaseWriteLock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Array的用法示例。


在下文中一共展示了Array.ReleaseWriteLock方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: AcquireReadWriteLocks_ReadWrite_Blocks

        public void AcquireReadWriteLocks_ReadWrite_Blocks()
        {
            var name = Guid.NewGuid().ToString();
            using (var sma = new Array<byte>(name, 10))
            {
                using (var smr = new Array<byte>(name))
                {
                    // Acquire write lock
                    sma.AcquireWriteLock();

                    // Should block (and fail to reset write signal)
                    Assert.IsFalse(smr.AcquireReadLock(10));

                    sma.ReleaseWriteLock();

                    smr.AcquireReadLock();

                    // Should block (and fail to reset read signal)
                    Assert.IsFalse(sma.AcquireWriteLock(10));

                    smr.ReleaseReadLock();
                }
            }
        }
開發者ID:yjqGitHub,項目名稱:SharedMemory,代碼行數:24,代碼來源:ArrayTests.cs

示例2: AcquireWriteLock_ReadWrite_LocksCorrectly

        public void AcquireWriteLock_ReadWrite_LocksCorrectly()
        {
            var name = Guid.NewGuid().ToString();
            Random r = new Random();
            int bufSize = 1024;
            byte[] data = new byte[bufSize];
            byte[] readBuf = new byte[bufSize];

            bool readIsFirst = false;
            bool readBlocked = false;
            int syncValue = 0;

            // Fill with random data
            r.NextBytes(data);

            using (var sma = new Array<byte>(name, bufSize))
            {
                // Acquire write lock early
                sma.AcquireWriteLock();
                using (var smr = new Array<byte>(name))
                {
                    var t1 = Task.Factory.StartNew(() =>
                        {
                            if (System.Threading.Interlocked.Exchange(ref syncValue, 1) == 0)
                                readIsFirst = true;
                            // Should block until write lock is released
                            smr.AcquireReadLock();
                            if (System.Threading.Interlocked.Exchange(ref syncValue, 3) == 4)
                                readBlocked = true;
                            smr.CopyTo(readBuf);
                            smr.ReleaseReadLock();
                        });

                    System.Threading.Thread.Sleep(10);

                    var t2 = Task.Factory.StartNew(() =>
                        {
                            var val = System.Threading.Interlocked.Exchange(ref syncValue, 2);
                            if (val == 0)
                                readIsFirst = false;
                            else if (val == 3)
                                readBlocked = false;
                            System.Threading.Thread.Sleep(10);
                            sma.Write(data);
                            System.Threading.Interlocked.Exchange(ref syncValue, 4);
                            sma.ReleaseWriteLock();
                        });

                    Task.WaitAll(t1, t2);

                    Assert.IsTrue(readIsFirst, "The read thread did not enter first.");
                    Assert.IsTrue(readBlocked, "The read thread did not block.");

                    // Check data was written before read
                    for (var i = 0; i < readBuf.Length; i++)
                    {
                        Assert.AreEqual(data[i], readBuf[i]);
                    }
                }
            }
        }
開發者ID:rstonkus,項目名稱:SharedMemory,代碼行數:61,代碼來源:ArrayTests.cs


注:本文中的System.Array.ReleaseWriteLock方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。