Skip to content

Commit

Permalink
Q8 - Implement mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
sikatikenmogne committed May 5, 2024
1 parent cf5b9d0 commit 9981c7c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Apply the concepts of threading and synchronization mechanisms to solve practica
5. [x] 5. Implement thread pools
6. [x] 6. Implement asynchronous delegates and callback procedures
7. [x] 7. Implement events
8. [ ] 8. Implement mutexes
8. [x] 8. Implement mutexes
9. [ ] 9. Implement monitors
10. [ ] 10. Implement semaphores
11. [ ] 11. Implement readerWriterLockSlim
Expand Down Expand Up @@ -222,6 +222,9 @@ Identify the critical section (code block) to protect.
Implement the appropriate synchronization mechanism (e.g., lock) to control access.
Demonstrate the use of this mechanism with two threads ("T1" and "T2") accessing the critical section.

#### D. Mutexes Implementation:
**💻 [related commit]()**

---
### Q9 - Synchronization:

Expand Down
50 changes: 46 additions & 4 deletions concurency-workshop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ internal class Program
// Q5 pool thread
public delegate void DelegateThreadFive(Object obj);

// Q6 Evt
// Q7 Evt
private delegate void DELG(object o);

// Q8 Synchronization
// Define a lock object
private static readonly object lockObject = new object();
private static int var = 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 All @@ -35,6 +40,7 @@ public static int addMethod(int i1, int i2)
return i1 + i2;
}

public delegate void SafeDelegate(object state);

static void Main(string[] args)
{
Expand Down Expand Up @@ -284,14 +290,50 @@ static void Main(string[] args)
System.Threading.Thread.Sleep(3000);
}

Console.Read();
// Console.Read();
Console.ResetColor();

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

Console.WriteLine();

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

Console.WriteLine("Main thread terminating...");
// <summary>
// Q8 - Synchronization
// </summary>

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

SafeDelegate safeDelegate = (object state) =>
{
lock (lockObject)
{
string name_thread = (string)state;
++var;
Console.WriteLine("Thread -> {0} -- var --> {1}", name_thread, var.ToString());
Thread.Sleep(2000);
}
};

Thread t4 = new Thread(new ParameterizedThreadStart(safeDelegate));
Thread t5 = new Thread(new ParameterizedThreadStart(safeDelegate));

t4.Start("T4");
t5.Start("T5");

t4.Join();
t5.Join();

Console.WriteLine();

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

Console.WriteLine();
// -------------------------------

Console.WriteLine("Main thread terminating...");

}
}
}

0 comments on commit 9981c7c

Please sign in to comment.