当前位置: 首页>>代码示例>>C#>>正文


C# SpinWait.SpinOnce方法代码示例

本文整理汇总了C#中SpinWait.SpinOnce方法的典型用法代码示例。如果您正苦于以下问题:C# SpinWait.SpinOnce方法的具体用法?C# SpinWait.SpinOnce怎么用?C# SpinWait.SpinOnce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SpinWait的用法示例。


在下文中一共展示了SpinWait.SpinOnce方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParticipateUntil

		public void ParticipateUntil (Func<bool> predicate)
		{
			SpinWait sw = new SpinWait ();
			
			while (!predicate ())
				sw.SpinOnce ();
		}
开发者ID:TheRealDuckboy,项目名称:mono-soc-2008,代码行数:7,代码来源:SchedulerProxy.cs

示例2: Run

        public static void Run()
        {
            var queue = new ConcurrentQueue<int>();

            // begin
            var producer = Task.Run(() =>
            {
                foreach (var value in Enumerable.Range(1, 10000))
                {
                    queue.Enqueue(value);
                }
            });

            var consumer = Task.Run(() =>
            {
                var spinWait = new SpinWait();
                var value = 0;
                while (value != 10000)
                {
                    if (!queue.TryDequeue(out value))
                    {
                        spinWait.SpinOnce();
                        continue;
                    }
                    Logger.Log("Value: {0}", value);
                }
            });

            Task.WaitAll(producer, consumer);
            // end
        }
开发者ID:Abc-Arbitrage,项目名称:Abc.NCrafts.Quiz,代码行数:31,代码来源:Answer1.cs

示例3: RunSpinWaitTests

        public static void RunSpinWaitTests()
        {
            SpinWait spinner = new SpinWait();

            spinner.SpinOnce();
            Assert.Equal(spinner.Count, 1);
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:7,代码来源:SpinWaitTests.cs

示例4: SlowEnter

		internal bool SlowEnter (StCancelArgs cargs)
		{
			int lastTime = (cargs.Timeout != Timeout.Infinite) ? Environment.TickCount : 0;
			StWaitBlock wb = null;
			do {
				int sc = spinCount;
#if NET_4_0
				var spinWait = new SpinWait ();
#endif
				do {
					if (state == FREE &&
					    Interlocked.CompareExchange (ref state, BUSY, FREE) == FREE) {
						return true;
					}
					if (top != null || sc-- <= 0) {
						break;
					}
#if NET_4_0
					spinWait.SpinOnce ();
#else
					Thread.SpinWait (1);
#endif
				} while (true);


				if (wb == null) {
					wb = new StWaitBlock (1);
				} else {
					wb.parker.Reset ();
				}

				do {
					StWaitBlock t;
					wb.next = t = top;
					if (Interlocked.CompareExchange (ref top, wb, t) == t) {
						break;
					}
				} while (true);

				if (TryEnter ()) {
					wb.parker.SelfCancel ();
					return true;
				}

				int ws = wb.parker.Park (cargs);

				if (ws != StParkStatus.Success) {
					cargs.ThrowIfException (ws);
					return false;
				}

				if (TryEnter ()) {
					return true;
				}
				
				if (!cargs.AdjustTimeout (ref lastTime)) {
					return false;
				}
			} while (true);
		}
开发者ID:duarten,项目名称:mono,代码行数:60,代码来源:StLock.cs

示例5: Enter

    public void Enter() {
        // If calling thread already owns the lock, increment recursion count and return
        Int32 threadId = Thread.CurrentThread.ManagedThreadId;
        if (threadId == m_owningThreadId) { m_recursion++; return; }

        // The calling thread doesn't own the lock, try to get it
        SpinWait spinwait = new SpinWait();
        for (Int32 spinCount = 0; spinCount < m_spincount; spinCount++) {
            // If the lock was free, this thread got it; set some state and return
            if (Interlocked.CompareExchange(ref m_waiters, 1, 0) == 0) goto GotLock;

            // Black magic: give other threads a chance to run
            // in hopes that the lock will be released
            spinwait.SpinOnce();
        }

        // Spinning is over and the lock was still not obtained, try one more time
        if (Interlocked.Increment(ref m_waiters) > 1) {
            // Still contention, this thread must wait
            m_waiterLock.WaitOne(); // Wait for the lock; performance hit
            // When this thread wakes, it owns the lock; set some state and return
        }

        GotLock:
        // When a thread gets the lock, we record its ID and
        // indicate that the thread owns the lock once
        m_owningThreadId = threadId; m_recursion = 1;
    }
开发者ID:ppatoria,项目名称:SoftwareDevelopment,代码行数:28,代码来源:SimpleHybridLock1.cs

示例6: SpinUntil

		public static bool SpinUntil (Func<bool> condition, int millisecondsTimeout)
		{
			SpinWait sw = new SpinWait ();
			Watch watch = Watch.StartNew ();

			while (!condition ()) {
				if (watch.ElapsedMilliseconds > millisecondsTimeout)
					return false;
				sw.SpinOnce ();
			}

			return true;
		}
开发者ID:Indifer,项目名称:Test,代码行数:13,代码来源:SpinWait.cs

示例7: ParallelForTestCase

		public void ParallelForTestCase ()
		{
			ParallelTestHelper.Repeat (() => {
				int[] expected = Enumerable.Range (1, 1000).Select ((e) => e * 2).ToArray ();
				int[] actual = Enumerable.Range (1, 1000).ToArray ();
				SpinWait sw = new SpinWait ();
				
				Parallel.For (0, actual.Length, (i) => { actual[i] *= 2; sw.SpinOnce (); });
				
				CollectionAssert.AreEquivalent (expected, actual, "#1, same");
				CollectionAssert.AreEqual (expected, actual, "#2, in order");
			});
		}
开发者ID:calumjiao,项目名称:Mono-Class-Libraries,代码行数:13,代码来源:ParallelTests.cs

示例8: EnterReadLock

		public void EnterReadLock()
		{
			SpinWait sw = new SpinWait();
			do
			{
				while ((rwlock & (RwWrite | RwWait)) > 0)
					sw.SpinOnce();

				if ((Interlocked.Add(ref rwlock, RwRead) & (RwWait | RwWait)) == 0)
					return;

				Interlocked.Add(ref rwlock, -RwRead);
			} while (true);
		}
开发者ID:925coder,项目名称:ravendb,代码行数:14,代码来源:SimpleRwLock.cs

示例9: LoopWork

 /// <summary>
 /// 循环工作
 /// </summary>
 private static void LoopWork()
 {
     var spinWait = new SpinWait();
     while (true)
     {
         lock (syncRoot)
         {
             foreach (var item in actions)
             {
                 item.Invoke();
             }
         }
         spinWait.SpinOnce();
     }
 }
开发者ID:cityjoy,项目名称:NetworkSocket,代码行数:18,代码来源:LoopWorker.cs

示例10: Dequeue

        public override Event Dequeue()
        {
            Event result = null;
            SpinWait spinWait = new SpinWait();

            while (!closing)
            {
                if (queue.TryDequeue(out result))
                {
                    break;
                }
                spinWait.SpinOnce();
            }

            return result;
        }
开发者ID:jaykang920,项目名称:x2clr,代码行数:16,代码来源:ConcurrentEventQueue.cs

示例11: SendWithTimeout

        public static int SendWithTimeout(this ZmqSocket socket, byte[] buffer, int length, TimeSpan timeout)
        {
            var stopwatch = Stopwatch.StartNew();
            var spinWait = new SpinWait();
            int result;
            do
            {
                result = socket.Send(buffer, length, SocketFlags.DontWait);
                if (socket.SendStatus != SendStatus.TryAgain)
                    break;

                spinWait.SpinOnce();
            }
            while (stopwatch.Elapsed <= timeout);

            return result;
        }
开发者ID:MarouenK,项目名称:Zebus,代码行数:17,代码来源:ZmqUtil.cs

示例12: SpinWaitForCondition

 public static bool SpinWaitForCondition(Func<bool> predicate, int timeout)
 {
     Thread.MemoryBarrier();
     var sw = new Stopwatch();
     var spin = new SpinWait();
     sw.Start();
     while (sw.ElapsedMilliseconds < timeout)
     {
         if (predicate())
         {
             sw.Stop();
             return true;
         }
         spin.SpinOnce();
     }
     sw.Stop();
     return false;
 }
开发者ID:AlonAmsalem,项目名称:ServiceBlocks,代码行数:18,代码来源:SpinWaitHelper.cs

示例13: ConsumeItems

        protected override void ConsumeItems( int count )
        {
            SpinWait spinWait = new SpinWait();
            int value;

            for ( int i = 0; i < count; )
            {
                if ( this.queue.TryDequeue( out value ) )
                {
                    i++;

                    spinWait.Reset();
                }
                else
                {
                    spinWait.SpinOnce();
                }
            }
        }
开发者ID:postsharp,项目名称:ThreadingTalk,代码行数:19,代码来源:TestSystemConcurrentQueue.cs

示例14: EnterWriteLock

		public void EnterWriteLock()
		{
			SpinWait sw = new SpinWait();
			do
			{
				int state = rwlock;
				if (state < RwWrite)
				{
					if (Interlocked.CompareExchange(ref rwlock, RwWrite, state) == state)
						return;
					state = rwlock;
				}
				// We register our interest in taking the Write lock (if upgradeable it's already done)
				while ((state & RwWait) == 0 && Interlocked.CompareExchange(ref rwlock, state | RwWait, state) != state)
					state = rwlock;
				// Before falling to sleep
				while (rwlock > RwWait)
					sw.SpinOnce();
			} while (true);
		}
开发者ID:925coder,项目名称:ravendb,代码行数:20,代码来源:SimpleRwLock.cs

示例15: EnterReadLock

		public void EnterReadLock (ref bool taken)
		{
			if (taken)
				throw new ArgumentException ("taken", "taken needs to be set to false");

			SpinWait sw = new SpinWait ();
			bool cont = true;

			do {
				while ((rwlock & (RwWrite | RwWait)) > 0)
					sw.SpinOnce ();

				try {}
				finally {
					if ((Interlocked.Add (ref rwlock, RwRead) & (RwWait | RwWait)) == 0) {
						taken = true;
						cont = false;
					} else {
						Interlocked.Add (ref rwlock, -RwRead);
					}
				}
			} while (cont);
		}
开发者ID:nlhepler,项目名称:mono,代码行数:23,代码来源:ReaderWriterLockSlimmer.cs


注:本文中的SpinWait.SpinOnce方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。