Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

A followup question can be found here here, based on this question and it's answer.

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself? Is a memory barrier needed at all at Unlock?

A followup question can be found here, based on this question and it's answer.

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself? Is a memory barrier needed at all at Unlock?

A followup question can be found here, based on this question and it's answer.

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself? Is a memory barrier needed at all at Unlock?
added 209 characters in body
Source Link

A followup question can be found here , based on this question and it's answer.

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself? Is a memory barrier needed at all at Unlock?

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself? Is a memory barrier needed at all at Unlock?

A followup question can be found here , based on this question and it's answer.

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself? Is a memory barrier needed at all at Unlock?
added 47 characters in body
Source Link

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself? Is a memory barrier needed at all at Unlock?

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself?

Considering:

  1. Interlocked.CompareExchange generates a memory barrier,
  2. Intended to be used with very fast operations (like generating an id),
  3. SpinWait starts sleeping in milliseconds after 10 or so spins (and does some other smart stuff),

What are characteristics (pros & cons) of this code?

public class SpinMutex
{
 const long Locked = 1;
 const long Unlocked = 0;
 long _locked;
 public SpinMutex() { _locked = Unlocked; }
 public void Lock()
 {
 SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked);
 }
 public bool Lock(int millisecondsTimeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, millisecondsTimeout);
 }
 public bool Lock(TimeSpan timeout)
 {
 return SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref _locked, Locked, Unlocked) == Locked, timeout);
 }
 public void Unlock()
 {
 Interlocked.Exchange(ref _locked, Unlocked);
 }
}

And some extra points to consider:

  • Does it help if SpinMutex be defined as struct instead of a class (I'm aware there would be hurdles with say readonly fields of struct type and the like)?
  • Should Interlocked.CompareExchange be used in Unlock too, instead of Interlocked.Exchange to ensure generating of a memory barrier? Or Interlocked.Exchange generates a memory barrier itself? Is a memory barrier needed at all at Unlock?
edited title
Link
Loading
Source Link
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /