top | item 32429412

(no title)

DenseComet | 3 years ago

Nope, the threads run in parallel. The variable x is mutably borrowed only once by the second thread. The variable a is immutably borrowed twice, so there is no issue.

discuss

order

theptip|3 years ago

Are you certain?

From the docs on https://doc.rust-lang.org/stable/std/thread/struct.Scope.htm...,

> If the join handle is dropped, the spawned thread will implicitly joined at the end of the scope.

Plus the comment says

> // We can even mutably borrow `x` here, // because no other threads are using it.

Which doesn’t line up with another thread having a read-only borrow on it.

Anyway not at my machine to test right now, I could be wrong.

Tuna-Fish|3 years ago

x is not used by the other thread, a is.

> If the join handle is dropped, the spawned thread will implicitly joined at the end of the scope.

"end of scope" here refers to the end of the closure, after the second thread is spawned.

You can trivially show that the threads exist in parallel by adding waits.

theptip|3 years ago

Oh never mind, I’m completely misreading. Thanks for the pointer.