Skip to content

Commit 6d48eb1

Browse files
committed
book: describe custom structs and multithreading
1 parent 425e939 commit 6d48eb1

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

book/src/main_event_loop.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ If you decide to share it, you user name will be printed on the console.
237237

238238
<div style="text-align:center"><img src="img/main_event_loop_ashpd.png" alt="Dialog requesting user information."/></div>
239239

240+
## Custom structs and multithreading
241+
242+
If you have a custom structure that needs to perform its function on a separate thread to avoid blocking, Rust's [Arc](https://doc.rust-lang.org/std/sync/struct.Arc.html) and [Mutex](https://doc.rust-lang.org/std/sync/struct.Mutex.html) are required for safe data transport. `Mutex` (mutual exclusion) allows data access from another thread while guaranteeing exclusivity of the request call. To request data from another thread, `lock()` is called on the `Mutext` to signal the attempt. But Rust's ownership system does not allow `Mutext` data to be safely exchanged between threads. To perform data transfer between threads, you need to utilize Rust's `Arc` structure. These `Arc`s (Atomic Reference Counting) are safe to share between the application threads. But keep in mind that this safety comes with the price and will affect the performance. In some cases it is cheaper to run operations on a single thread without spending resources on locks and reference counting.
243+
240244
## Tokio
241245

242246
[`tokio`](https://docs.rs/tokio/latest/tokio/) is Rust's most popular asynchronous platform.

0 commit comments

Comments
 (0)