Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Whats iS THE Problem Of MY code in Monitor class For Resolve of Race Condition

Whats iS THE Problem Of MY code in Monitor class For Resolve of Race Condition

Scheduled Pinned Locked Moved C#
csharplinqgraphicshelplounge
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shahramkeyboard
    wrote on last edited by
    #1

    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);
    
    L 1 Reply Last reply
    0
    • S shahramkeyboard

      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);
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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).


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups