@@ -134,7 +134,8 @@ public abstract class CoroutineDispatcher :
134
134
*
135
135
* One of the common patterns is confining the execution of specific tasks to a sequential execution in background
136
136
* with `limitedParallelism(1)` invocation.
137
- * For that purpose, the implementation guarantees that tasks are executed sequentially and that a happens-before relation
137
+ * For that purpose, the implementation guarantees that sections of code between suspensions
138
+ * are executed sequentially and that a happens-before relation
138
139
* is established between them:
139
140
*
140
141
* ```
@@ -149,6 +150,42 @@ public abstract class CoroutineDispatcher :
149
150
* ```
150
151
* Note that there is no guarantee that the underlying system thread will always be the same.
151
152
*
153
+ * #### It is not a mutex!
154
+ *
155
+ * **Pitfall**: [limitedParallelism] limits how many threads can execute some code in parallel,
156
+ * but does not limit how many coroutines execute concurrently!
157
+ *
158
+ * For example:
159
+ *
160
+ * ```
161
+ * val notAMutex = Dispatchers.Default.limitedParallelism(1)
162
+ *
163
+ * repeat(3) {
164
+ * launch(notAMutex) {
165
+ * println("Coroutine $it entering...")
166
+ * delay(20.milliseconds)
167
+ * println("Coroutine $it leaving.")
168
+ * }
169
+ * }
170
+ * ```
171
+ *
172
+ * The output will be similar to this:
173
+ *
174
+ * ```
175
+ * Coroutine 0 entering...
176
+ * Coroutine 1 entering...
177
+ * Coroutine 2 entering...
178
+ * Coroutine 0 leaving.
179
+ * Coroutine 1 leaving.
180
+ * Coroutine 2 leaving.
181
+ * ```
182
+ *
183
+ * This means that coroutines are not guaranteed to run to completion before the dispatcher starts executing
184
+ * code from another coroutine.
185
+ * The only guarantee in this example is that two `println` calls will not occur in several threads simultaneously.
186
+ *
187
+ * Use a [kotlinx.coroutines.sync.Mutex] or a [kotlinx.coroutines.sync.Semaphore] for limiting concurrency.
188
+ *
152
189
* ### Dispatchers.IO
153
190
*
154
191
* `Dispatcher.IO` is considered _elastic_ for the purposes of limited parallelism -- the sum of
0 commit comments