Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace RaceCondition
- {
- class Program
- {
- static object locker = new object();
- private static int counter;
- static void Main(string[] args)
- {
- new Thread(PrintStar).Start();
- new Thread(PrintPlus).Start();
- Console.WriteLine();
- }
- static void PrintStar()
- {
- lock (locker) // Thread safe code
- {
- for (counter = 0; counter < 5; counter++)
- {
- Console.Write(" *(" + counter + ")\t");
- }
- }
- }
- static void PrintPlus()
- {
- lock (locker) // Thread safe code
- {
- for (counter = 0; counter < 5; counter++)
- {
- Console.Write(" +(" + counter + ")\t");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement