Skip to content

Commit 0597644

Browse files
imba-tjdBillWagner
authored andcommitted
Move snippets (#2419)
* Move snippets * Apply suggestions from code review Co-Authored-By: imba-tjd <[email protected]>
1 parent 692da4c commit 0597644

File tree

1 file changed

+4
-139
lines changed

1 file changed

+4
-139
lines changed

xml/System.Threading/SpinWait.xml

Lines changed: 4 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -172,144 +172,9 @@
172172
## Examples
173173
The following is an example of using SpinWait in a simple lock-free stack implementation. (This is just an example. If an efficient, thread-safe stack is needed, consider using ConcurrentStack.)
174174
175-
`// C#`
176-
177-
`public class LockFreeStack<T>`
178-
179-
`{`
180-
181-
`private volatile Node m_head;`
182-
183-
`private class Node { public Node Next; public T Value; }`
184-
185-
`public void Push(T item)`
186-
187-
`{`
188-
189-
`var spin = new SpinWait();`
190-
191-
`Node node = new Node { Value = item }, head;`
192-
193-
`while (true)`
194-
195-
`{`
196-
197-
`head = m_head;`
198-
199-
`node.Next = head;`
200-
201-
`if (Interlocked.CompareExchange(ref m_head, node, head) == head) break;`
202-
203-
`spin.SpinOnce();`
204-
205-
`}`
206-
207-
`}`
208-
209-
`public bool TryPop(out T result)`
210-
211-
`{`
212-
213-
`result = default(T);`
214-
215-
`var spin = new SpinWait();`
216-
217-
`Node head;`
218-
219-
`while (true)`
220-
221-
`{`
222-
223-
`head = m_head;`
224-
225-
`if (head == null) return false;`
226-
227-
`if (Interlocked.CompareExchange(ref m_head, head.Next, head) == head)`
228-
229-
`{`
230-
231-
`result = head.Value;`
232-
233-
`return true;`
234-
235-
`}`
236-
237-
`spin.SpinOnce();`
238-
239-
`}`
240-
241-
`}`
242-
243-
`}`
244-
245-
`' Visual Basic`
246-
247-
`Public Class LockFreeStack(Of T)`
248-
249-
`Private m_head As Node`
250-
251-
`Private Class Node`
252-
253-
`Public [Next] As Node`
254-
255-
`Public Value As T`
256-
257-
`End Class`
258-
259-
`Public Sub Push(ByVal item As T)`
260-
261-
`Dim spin As New SpinWait()`
262-
263-
`Dim head As Node, node As New Node With {.Value = item}`
264-
265-
`While True`
266-
267-
`Thread.MemoryBarrier()`
268-
269-
`head = m_head`
270-
271-
`node.Next = head`
272-
273-
`If Interlocked.CompareExchange(m_head, node, head) Is head Then Exit While`
274-
275-
`spin.SpinOnce()`
276-
277-
`End While`
278-
279-
`End Sub`
280-
281-
`Public Function TryPop(ByRef result As T) As Boolean`
282-
283-
`result = CType(Nothing, T)`
284-
285-
`Dim spin As New SpinWait()`
286-
287-
`Dim head As Node`
288-
289-
`While True`
290-
291-
`Thread.MemoryBarrier()`
292-
293-
`head = m_head`
294-
295-
`If head Is Nothing Then Return False`
296-
297-
`If Interlocked.CompareExchange(m_head, head.Next, head) Is head Then`
298-
299-
`result = head.Value`
300-
301-
`Return True`
302-
303-
`End If`
304-
305-
`spin.SpinOnce()`
306-
307-
`End While`
308-
309-
`End Function`
310-
311-
`End Class`
312-
175+
[!code-csharp[System.Threading.SpinWait.Reset#1](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.threading.spinwait/cs/reset.cs#01)]
176+
[!code-vb[System.Threading.SpinWait.Reset#1](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.threading.spinwait/vb/reset.vb#01)]
177+
313178
]]></format>
314179
</remarks>
315180
<related type="Article" href="https://msdn.microsoft.com/library/36012f42-34e5-4f86-adf4-973f433ed6c6">SpinWait</related>
@@ -533,4 +398,4 @@
533398
</Docs>
534399
</Member>
535400
</Members>
536-
</Type>
401+
</Type>

0 commit comments

Comments
 (0)