Skip to content

Instantly share code, notes, and snippets.

@Markeli
Created November 8, 2016 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Markeli/c2d27dcc6757de1b05e21c22eece0f6c to your computer and use it in GitHub Desktop.
Save Markeli/c2d27dcc6757de1b05e21c22eece0f6c to your computer and use it in GitHub Desktop.
StackOverflow:RU:588475
public void DoWork()
{
//Your code goes here
Task.Factory.StartNew(() =>
{
var coat = human.Coar("2");
if (!coat.SetOwner(human)) return;
// some logic
coat.SetOwner(null);
});
}
public class Human
{
//your code here
}
public class Coat
{
public Human Owner {get; private set; }
private object _lockObject = new object();
public bool SetOwner(Human owner)
{
if (Owner != null) return false;
lock(_lockObject)
{
if (Owner != null) return false;
Owner = owner;
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment