title: In C# would it be better to use Queue.Synchronized or lock() for thread safety?
body: <p>I have a Queue object that I need to ensure is thread-safe. Would it be better to use a lock object like this:</p>
  
  <pre><code>lock(myLockObject)
  {
  //do stuff with the queue
  }
  </code></pre>
  
  <p>Or is it recommended to use Queue.Synchronized like this:</p>
  
  <pre><code>Queue.Synchronized(myQueue).whatever_i_want_to_do();
  </code></pre>
  
  <p>From reading the MSDN docs it says I should use Queue.Synchronized to make it thread-safe, but then it gives an example using a lock object. From the MSDN article:</p>
  
  <blockquote>
    <p>To guarantee the thread safety of the
    Queue, all operations must be done
    through this wrapper only.</p>
    
    <p>Enumerating through a collection is
    intrinsically not a thread-safe
    procedure. Even when a collection is
    synchronized, other threads can still
    modify the collection, which causes
    the enumerator to throw an exception.
    To guarantee thread safety during
    enumeration, you can either lock the
    collection during the entire
    enumeration or catch the exceptions
    resulting from changes made by other
    threads.</p>
  </blockquote>
  
  <p>If calling Synchronized() doesn't ensure thread-safety what's the point of it? Am I missing something here?</p>