Questions tagged [spinwait]

A spin-wait loop is a technique used in multithreaded applications whereby one thread waits for other threads for protecting a critical section, for barriers, or for other synchronizations.

A spin-wait loop is a technique used in multithreaded applications whereby one thread waits for other threads for protecting a critical section, for barriers, or for other synchronizations.

References

16 questions
70
votes
3 answers

Why there is a Thread.Sleep(1) in .NET internal Hashtable?

Recently I was reading implementation of .NET Hashtable and encountered piece of code that I don't understand. Part of the code is: int num3 = 0; int num4; do { num4 = this.version; bucket = bucketArray[index]; if (++num3 % 8 == 0) …
Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
10
votes
3 answers

Are there any examples in the .Net framework that use spinlock or spinwait?

I had a look at the concurrent collections but they appear to use normal locking underneath the hood. Are there any good examples in the .Net framework that use this locking construct? I initially looked at ConcurrentDictionary. I saw it was using…
uriDium
  • 13,110
  • 20
  • 78
  • 138
10
votes
2 answers

Should interlocked implementations based on CompareExchange use SpinWait?

Below is an implementation of an interlocked method based on Interlocked.CompareExchange. Is it advisable for this code to use a SpinWait spin before reiterating? public static bool AddIfLessThan(ref int location, int value, int comparison) { …
Timo
  • 7,992
  • 4
  • 49
  • 67
3
votes
3 answers

Calling thread.sleep() while waiting for a new thread to spawn

I suppose this question can be boiled down to "SpinWait vs. Block?", but I figured there may be a more interesting answer as to why nearly every C# threading tutorial suggests the following call: Thread newThread = new Thread(new…
wger
  • 31
  • 2
3
votes
1 answer

SpinWait in lockless update

While reading Albahari's Threading in C#, I've noticed that the "lock free update" pattern uses a SpinWait at the end of the cycle: static void LockFreeUpdate (ref T field, Func updateFunction) where T : class { var spinWait = new…
Lou
  • 4,244
  • 3
  • 33
  • 72
3
votes
0 answers

Why do you need SpinWait in LockFreeUpdate sample from Albahari's "Threading in C#"

I am working on small Ref abstraction with lock-free optimistic Update method (supposed to work with immutable T data structures, immutable trees in particular). It is based on Albahari's "Threading in C#" LockFreeUpdate method in SpinWait usage…
dadhi
  • 4,807
  • 19
  • 25
3
votes
2 answers

How is NET 4.0 SpinWait method different to pre-4.0 SpinWait()?

MSDN "Thread-Safe Collections .NET Framework 4" states: "Some of the concurrent collection types use lightweight synchronization mechanisms such as SpinLock, SpinWait, SemaphoreSlim, and CountdownEvent, which are new in the .NET Framework…
Fulproof
  • 4,466
  • 6
  • 30
  • 50
2
votes
2 answers

SpinWait.SpinUntil taking MUCH longer than timeout to exit while waiting for Selnium element to exist

I have a relatively simple method to wait until an element exists and is displayed. The method handles the situation where more than a single element is returned for the given By (usually we only expect one of them to be displayed but in any case…
Ory Zaidenvorm
  • 896
  • 1
  • 6
  • 20
2
votes
1 answer

Spin wait C++11

I have the following struct struct info { unsigned long a; unsigned long b; }; atomic data; used by a writer thread and a reader thread. The reader has to respond to the new values as fast as possible. To do that I’ve implemented the…
toco
  • 29
  • 1
2
votes
1 answer

What is the equivalent of Windows .NET (C++) SpinWait on Linux and Mac OS X?

Windows .NET (C++) provides SpinWait for Hyper-threading friendly busy waiting with YIELD/PAUSE instructions. What is the equivalent function on Linux and Mac OS X? If a system call isn't available, how can an equivalent be implemented in user…
Coder
  • 441
  • 2
  • 17
2
votes
2 answers

Power preserving SpinWait

I have a polling loop in C# that needs to poll every 100 microseconds on average < EDIT> (given of course that there is no excessive preemptive thread context switch carried out by Windows due to core shortage) < /EDIT>. As there is no time for a…
Jack Wester
  • 5,170
  • 3
  • 28
  • 47
1
vote
1 answer

Why have AMD-CPUs such a silly PAUSE-timing

I've developed a monitor-object like that of Java for C++ with some improvements. The major improvement is that there's not only a spin-loop for locking and unlocking but also for waiting on an event. In this case you don't have to lock the mutex…
Bonita Montero
  • 2,817
  • 9
  • 22
1
vote
1 answer

Random Numbers in For Loops

Hi I have searched all over the internet for this answer and please let me explain before i get into the actual problem. it may seem simple to but the only choices that really helped me was Thread.Sleep and Thread.SpinWait but it basically stops my…
1
vote
1 answer

nutch crawling gets stuck in spinwaiting or active. how to reduce fetch cycle?

I am using nutch 2.1 and crawling a site. The problem is that the crawler keeps showing fetching url spinwaiting/active and since the fetching takes so much time the connection to mysql gets timedout. How can i reduce the number of fetches at a time…
peter
  • 3,411
  • 5
  • 24
  • 27
-1
votes
1 answer

Thread.SpinWait seems to be affected by the main thread

I’m using Thread.SpinWait on a special thread to create a near-exact 2ms delay with minimal chance of a context switch, needed for precise hardware communication timing. But using a profiler, I’ve noticed that if the main thread does something that…
1
2