Skip to content

Commit

Permalink
The lock statement (csharp-13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimatay committed Jun 20, 2024
1 parent 54f59e7 commit f6896e8
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions TheLockStatement/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,57 @@
// The lock statement - ensure exclusive access to a shared resource
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock

// Lock Class
// src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs
// https://learn.microsoft.com/en-us/dotnet/api/system.threading.lock?view=net-9.0

Singleton instance = Singleton.Instance;

Console.WriteLine(instance.CurrentDateTime);

Task.Factory.StartNew(()=>{
// What is the difference between Task.Run() and Task.Factory.StartNew()
// https://stackoverflow.com/questions/38423472/what-is-the-difference-between-task-run-and-task-factory-startnew

Task.Factory.StartNew(() =>
{
Singleton instance2 = Singleton.Instance;
Console.WriteLine(instance2.CurrentDateTime);
new Task(()=>{
new Task(() =>
{
Thread.Sleep(10000);
}, TaskCreationOptions.AttachedToParent).Start();
new Task(() =>
{
Thread.Sleep(5000);
Singleton instance2 = Singleton.Instance;
Console.WriteLine(instance2.CurrentDateTime);
}, TaskCreationOptions.AttachedToParent).Start();
});


public class Singleton
{
private readonly System.Threading.Lock _lock = new();
private static Singleton instance = null;

public DateTime CurrentDateTime => _currentDateTime;
private DateTime _currentDateTime;

private Singleton() { }

public static Singleton Instance
private Singleton() { }
public static Singleton Instance
{
get
get
{
if(_lock)
if (_lock)
{
if (instance == null)
if (instance == null)
{
instance = new();
instance._currentDateTime = DateTime.Now;
}
}
}

return instance;
}
return instance;
}
}

}

0 comments on commit f6896e8

Please sign in to comment.