Is it safe to use lock() with Tasks?
<p>I read here:</p>

<p><a href="https://stackoverflow.com/questions/16549771/net-async-can-a-single-thread-time-slice-between-tasks">.NET async, can a single thread time-slice between tasks?</a></p>

<p>that, unless you explicitly use async/await, tasks will not "time-slice" on the same thread in the backend thread-pool. Is this guaranteed? Or merely a side effect of the current implementation of the TPL?</p>

<p>If not guaranteed, it would cause problems with using <code>lock()</code>:</p>

<p>Consider two Tasks which access a method that <code>locks</code> on a full SerialPort transaction (send a message and receive, or timeout) before releasing. If time-slicing occurs on the same thread and the SerialPort access is slow enough, the <code>lock</code> would fail to do its job (letting both calls through, because they are technically on the same thread).</p>