M
mwindham
Guest
I am having trouble converting the following to C++(.Net). First trouble starts around
Node node = new Node { Value = item }, head;
in
public class LockFreeStack<T>
{
private volatile Node m_head;
private class Node { public Node Next; public T Value; }
public void Push(T item)
{
var spin = new SpinWait();
Node node = new Node { Value = item }, head;
while (true)
{
head = m_head;
node.Next = head;
if (Interlocked.CompareExchange(ref m_head, node, head) == head) break;
spin.SpinOnce();
}
}
public bool TryPop(out T result)
{
result = default(T);
var spin = new SpinWait();
Node head;
while (true)
{
head = m_head;
if (head == null) return false;
if (Interlocked.CompareExchange(ref m_head, head.Next, head) == head)
{
result = head.Value;
which is from SpinWait
This what cs to cpp converter from Tangible older edition did:
generic<typename T>
public ref class LockFreeStack
{
private:
ref class Node
{
public:
Node ^Next;
T ^Value;
};
private:
volatile Node ^m_head;
public:
void Push(T item)
{
auto spin = gcnew SpinWait();
Node ^node = gcnew Node {Value = item}, ^head;
while (true)
{
head = m_head;
node->Next = head;
if (Interlocked::CompareExchange(m_head, node, head) == head)
{
break;
}
spin->SpinOnce();
}
Continue reading...
Node node = new Node { Value = item }, head;
in
public class LockFreeStack<T>
{
private volatile Node m_head;
private class Node { public Node Next; public T Value; }
public void Push(T item)
{
var spin = new SpinWait();
Node node = new Node { Value = item }, head;
while (true)
{
head = m_head;
node.Next = head;
if (Interlocked.CompareExchange(ref m_head, node, head) == head) break;
spin.SpinOnce();
}
}
public bool TryPop(out T result)
{
result = default(T);
var spin = new SpinWait();
Node head;
while (true)
{
head = m_head;
if (head == null) return false;
if (Interlocked.CompareExchange(ref m_head, head.Next, head) == head)
{
result = head.Value;
which is from SpinWait
This what cs to cpp converter from Tangible older edition did:
generic<typename T>
public ref class LockFreeStack
{
private:
ref class Node
{
public:
Node ^Next;
T ^Value;
};
private:
volatile Node ^m_head;
public:
void Push(T item)
{
auto spin = gcnew SpinWait();
Node ^node = gcnew Node {Value = item}, ^head;
while (true)
{
head = m_head;
node->Next = head;
if (Interlocked::CompareExchange(m_head, node, head) == head)
{
break;
}
spin->SpinOnce();
}
Continue reading...