Skip to content

Commit

Permalink
Q11 - Implement readerWriterLockSlim
Browse files Browse the repository at this point in the history
  • Loading branch information
sikatikenmogne committed May 5, 2024
1 parent eb24d56 commit a78c4d6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Apply the concepts of threading and synchronization mechanisms to solve practica
8. [x] 8. Implement mutexes
9. [x] 9. Implement monitors
10. [x] 10. Implement semaphores
11. [ ] 11. Implement readerWriterLockSlim
11. [x] 11. Implement readerWriterLockSlim

Duration: 6 hours

Expand Down Expand Up @@ -286,3 +286,6 @@ Choose the appropriate synchronization mechanism (e.g., ReaderWriterLockSlim).
Implement the mechanism with four threads:
Two in read mode.
Two in write mode (one initializes an array with odd values, the other with even values).

#### D. ReaderWriterLockSlim Implementation:
**💻 [related commit]()**
72 changes: 72 additions & 0 deletions concurency-workshop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ internal class Program
private static string simu_cnx_db = "Utilisation de la base de données";
private static SemaphoreSlim semaphore = new SemaphoreSlim(2, 2);

// Q11 Synchronization
static ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();
static int sharedResource = 0;

// <summary>
// Q1 - Delegate* Let the method 'int method (int v1, int v2)'. This method adds two values and returns the result. Write the delegate who will invoke this method
// </summary>
Expand Down Expand Up @@ -452,7 +456,75 @@ static void Main(string[] args)
Console.WriteLine("Main thread terminating...");

// -------------------------------

// <summary>
// Q11 - Synchronization
// </summary>

Console.WriteLine("Q11 - =========Synchronization=========");
Console.WriteLine();

Thread writer1 = new Thread(Write);
writer1.Name = "Writer 1";
Thread writer2 = new Thread(Write);
writer2.Name = "Writer 2";

Thread reader1 = new Thread(Read);
reader1.Name = "Reader 1";
Thread reader2 = new Thread(Read);
reader2.Name = "Reader 2";

writer1.Start();
reader1.Start();
writer2.Start();
reader2.Start();

writer1.Join();
reader1.Join();
writer2.Join();
reader2.Join();

Console.WriteLine();
Console.WriteLine("Q11 - ==============END=============");
Console.WriteLine();

// -------------------------------
}


static void Write()
{
rwl.EnterWriteLock();
try
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{Thread.CurrentThread.Name} is writing to the shared resource.");
sharedResource = new Random().Next(1, 100);
Console.WriteLine($"{Thread.CurrentThread.Name} has written {sharedResource} to the shared resource.");
Console.ResetColor();
Thread.Sleep(1000);
}
finally
{
rwl.ExitWriteLock();
}
}

static void Read()
{
rwl.EnterReadLock();
try
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{Thread.CurrentThread.Name} is reading from the shared resource.");
Console.WriteLine($"{Thread.CurrentThread.Name} has read {sharedResource} from the shared resource.");
Console.ResetColor();
Thread.Sleep(1000);
}
finally
{
rwl.ExitReadLock();
}
}
}
}

0 comments on commit a78c4d6

Please sign in to comment.