Skip to content

Commit 03f9b00

Browse files
committed
Altering the Send and Sync section intro and adding some code examples
1 parent fc4a620 commit 03f9b00

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

src/concurrency.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,61 @@ r[concurrency.send-and-sync]
1212
r[concurrency.send-and-sync.intro]
1313
The [`Send`] and [`Sync`] traits are [unsafe traits] used by the Rust type system to track which types can be safely used across thread boundaries.
1414

15-
These traits are [marker traits] with no methods. Implementing them asserts that a type has the intrinsic properties required for safe concurrent use. The compiler automatically implements these traits for most types when possible, but they can also be implemented manually. Providing an incorrect manual implementation can cause [undefined behavior].
15+
These traits are [marker traits] with no methods. Implementing them (whether manually or via the compiler's automatic implementation) asserts that a type has the intrinsic properties required for safe concurrent use.
16+
17+
Library functions that require types that can be sent across threads require [`Send`].
18+
```rust
19+
// This will compile successfully
20+
// A type is `Send` if it can be safely transferred to another thread.
21+
fn assert_send<T: Send>() {}
22+
23+
fn main() {
24+
assert_send::<i32>(); // primitive types are Send
25+
assert_send::<Vec<u8>>(); // Vec<T> is Send if T is Send
26+
}
27+
```
28+
29+
```rust
30+
// This will not compile
31+
// A type containing `Rc<T>` is not `Send`.
32+
use std::rc::Rc;
33+
34+
fn assert_send<T: Send>() {}
35+
36+
fn main() {
37+
let _x = Rc::new(1);
38+
assert_send::<Rc<i32>>(); //~ error[E0277] `Rc<i32>` cannot be sent between threads safely
39+
}
40+
```
41+
42+
Library functions that require types that can be accessed concurrently from multiple threads require [`Sync`].
43+
```rust
44+
// This will compile successfully
45+
// A type is `Sync` if it can be safely referenced from multiple threads.
46+
fn assert_sync<T: Sync>() {}
47+
48+
fn main() {
49+
assert_sync::<i32>(); // i32 is Sync
50+
assert_sync::<&'static str>(); // string slices are Sync
51+
assert_sync::<std::sync::Arc<i32>>(); // Arc<T> is Sync if T is Sync
52+
}
53+
```
54+
55+
```rust
56+
// This will not compile
57+
// A type containing `Cell<T>` is not `Sync`.
58+
use std::cell::Cell;
59+
60+
fn assert_sync<T: Sync>() {}
61+
62+
fn main() {
63+
assert_sync::<Cell<i32>>(); //~ error[E0277] `*const i32` cannot be shared between threads safely
64+
}
65+
```
1666

1767
[concurrent programs]: glossary.md#concurrent-program
1868
[data races]: glossary.md#data-race
1969
[`Send`]: special-types-and-traits.md#Send
2070
[`Sync`]: special-types-and-traits.md#Sync
2171
[unsafe traits]: items/traits.md#unsafe-traits
2272
[marker traits]: glossary.md#marker-trait
23-
[undefined behavior]: glossary.md#undefined-behavior

0 commit comments

Comments
 (0)