title: C# threading - Lock Object
body: <p>I am trying to lock a "boxed" object in a c# app, is this not possible?</p>
  
  <pre><code>    class t
      {
          System.Object t_x = new object();
  
          public t(int p)
          {
              t_x = p;
  
          }
  
          public void w()
          {
              lock (t_x)
              {
                  for (int i = 0; i &lt; 4; i++)
                  {
  
                      {
                          t_x = ((int)t_x) + 1;
                          Console.WriteLine(t_x);
                          Thread.Sleep(1000);
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>In another class I can start 2 threads:</p>
  
  <pre><code>        Thread b1 = new Thread(new ThreadStart(t1.w));
          b1.Start();
          Thread b2 = new Thread(new ThreadStart(t1.w));
          b2.Start();
  </code></pre>
  
  <p>However the portion is not locked.
  When I lock an arbitrary object (i.e. one created and not modified as object a=new object()) it locks well.
  Is boxing operation somehow "depromotes" my Object??</p>