Skip to content

Commit

Permalink
Book: Add clarification around creating tokio runtime (#1546)
Browse files Browse the repository at this point in the history
Off the back of gtk-rs/gtk-rs-core#1251,
this adds some additional detail to the Tokio section of the main event loop page that explains *why* the runtime is manually created, and the reasons to avoid doing it the 'normal' way.
  • Loading branch information
JakeStanger authored Dec 17, 2023
1 parent 2ed0557 commit 42772bc
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion book/src/main_event_loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ cargo add tokio@1 --features rt-multi-thread
```

Since we already run the `glib` main loop on our main thread, we don't want to run the `tokio` runtime there.
Let's bind it to a static variable and initialize it lazily.
For this reason, we **avoid** using the `#[tokio::main]` macro or using a top-level `block_on` call.
Doing this will block one of the runtime's threads with the GLib main loop, which is a waste of resources and a potential source of strange bugs.

Instead, the runtime is manually created and used where needed. Let's bind it to a static variable and initialize it lazily.

Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/main_event_loop/9/main.rs">listings/main_event_loop/9/main.rs</a>

Expand Down

0 comments on commit 42772bc

Please sign in to comment.