Created
November 8, 2016 20:34
-
-
Save Markeli/c2d27dcc6757de1b05e21c22eece0f6c to your computer and use it in GitHub Desktop.
StackOverflow:RU:588475
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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