title: Do i need to synchronize read and write actions to the same SerialPort instance?
body: <p>Here's my scenario: I need to read from and write to the same serial port in multiple threads. Instead of creating one SerialPort in each thread, which would cause 'access denied' error, i use kind of global cache to do the job. For each serial port, i keep only one SerialPort object.</p>
  
  <pre><code>private static object locker = new object();
  internal static Dictionary&lt;int, SerialPort&gt; SerialPorts = new Dictionary&lt;int, SerialPort&gt;();
  
  private SerialPort GetSerialPort(string port)
      {
          lock (locker)
          {
              if (!SerialPorts.ContainsKey(port))
              {
                  SerialPort sp = new SerialPort(port, baudrate);
  
                  sp.Open();
  
                  SerialPorts[key] = sp;
              }
          }
  
          return SerialPorts[key];
      }
  </code></pre>
  
  <p>In read and write actions, I call GetSerialPort first to get the SerialPort object. Then the respective ReadByte and Write method is called. Now my question is: without any special handling, is it safe to just have multiple threads calling either ReadByte or Write methods at the same time?</p>