本文整理汇总了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 ();
}
示例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
}
示例3: RunSpinWaitTests
public static void RunSpinWaitTests()
{
SpinWait spinner = new SpinWait();
spinner.SpinOnce();
Assert.Equal(spinner.Count, 1);
}
示例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);
}
示例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;
}
示例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;
}
示例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");
});
}
示例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);
}
示例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();
}
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
}
}
示例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);
}
示例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);
}