Whats iS THE Problem Of MY code in Monitor class For Resolve of Race Condition
-
hi my Code IS:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace RaceCondition
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}string s=" "; Thread t1, t2; private void button1\_Click(object sender, EventArgs e) { t1 = new Thread(f1); t2 = new Thread(f2); t1.Start(); t2.Start(); while (t1.IsAlive||t2.IsAlive) { Thread.Sleep(50); } for (int i = 0; i < s.Length; i++) { if (s\[i\]=='n') { listBox1.Items.Add(""); } else { listBox1.Items\[listBox1.Items.Count-1\]+=s\[i\].ToString(); } } } Random r = new Random(); void f1() { Thread.Sleep(r.Next(2)); System.Threading.Monitor.Enter(s); for (int i = 1; i <= 100; i+=2) { Thread.Sleep(r.Next(2)); s += 'n'; Thread.Sleep(r.Next(2)); s += "T1 "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); System.Threading.Monitor.Exit(s); } Thread.Sleep(r.Next(2)); } void f2() { Thread.Sleep(r.Next(2)); System.Threading.Monitor.Enter(s); for (int i = 2; i <= 100; i += 2) { Thread.Sleep(r.Next(2)); s += 'n'; Thread.Sleep(r.Next(2)); s += "T2 "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); System.Threading.Monitor.Exit(s);
-
hi my Code IS:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace RaceCondition
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}string s=" "; Thread t1, t2; private void button1\_Click(object sender, EventArgs e) { t1 = new Thread(f1); t2 = new Thread(f2); t1.Start(); t2.Start(); while (t1.IsAlive||t2.IsAlive) { Thread.Sleep(50); } for (int i = 0; i < s.Length; i++) { if (s\[i\]=='n') { listBox1.Items.Add(""); } else { listBox1.Items\[listBox1.Items.Count-1\]+=s\[i\].ToString(); } } } Random r = new Random(); void f1() { Thread.Sleep(r.Next(2)); System.Threading.Monitor.Enter(s); for (int i = 1; i <= 100; i+=2) { Thread.Sleep(r.Next(2)); s += 'n'; Thread.Sleep(r.Next(2)); s += "T1 "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); System.Threading.Monitor.Exit(s); } Thread.Sleep(r.Next(2)); } void f2() { Thread.Sleep(r.Next(2)); System.Threading.Monitor.Enter(s); for (int i = 2; i <= 100; i += 2) { Thread.Sleep(r.Next(2)); s += 'n'; Thread.Sleep(r.Next(2)); s += "T2 "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); s += i.ToString() + " "; Thread.Sleep(r.Next(2)); System.Threading.Monitor.Exit(s);
Hi, I haven't studied that in any detail, I do have a few comments though: 1. you have a Monitor.Exit inside a for loop, whereas the Monitor.Enter is executed only once? That means most of the loop is not protected by the monitor. 2. Thread.Sleep() takes an integer parameter representing the requested delay in milliseconds. However, its resolution is really system-dependent, and never that good. It typically will round up to a multiple of some 15 milliseconds. If you want details, I suggest you read Timer surprises, and how to avoid them[^]. 3. Rather than constructing a poll loop to wait for the end of one or more threads, I suggest you read up on
Thread.Join()
:)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).