Skip to Content
Async Rust ―高いパフォーマンスと安全性を両立するRustによる非同期処理
book

Async Rust ―高いパフォーマンスと安全性を両立するRustによる非同期処理

by Maxwell Flitton, Caroline Morton, 中田 秀基
July 2025
Intermediate to advanced
284 pages
4h 4m
Japanese
O'Reilly Japan, Inc.
Content preview from Async Rust ―高いパフォーマンスと安全性を両立するRustによる非同期処理

7章Tokioのカスタマイズ

本書ではこれまでもTokioを例題に使ってきた。それはTokioが最も広く使われているからだけでなく、構文がきれいでマクロ1つで非同期コード例を実行できるからだ。非同期なRustのコードを扱っていれば、Tokioが使われているのを見ることになる。これまで、Tokioを使用する際には、標準のランタイムを構築し、それに非同期タスクを送っていた。本章ではTokioランタイムをカスタマイズし、さまざまな構成のスレッド集合を作成し、タスクの実行を細粒度に制御する方法を示す。また、非同期ランタイム内でのunsafeなスレッドへのアクセスが実際に安全なのかどうかをテストする。最後に、非同期ランタイムが終了した際にきれいにシャットダウンする方法についても説明する。

本章を読めば、特定の問題を解決するためにTokioランタイムを設定できるようになる。タスクを実行するスレッドを特定し、スレッド固有の状態に依存できるようにすることで、データにアクセスする際にデータをロックする必要を削減できる。最後に、[Ctrl]-[C]をタイプした際、もしくはプログラムに対してシグナルが送られた際の、プログラムの終了方法を指定できるようになる。まずはTokioランタイムの作成から始めよう。

本章ではTokioを好みに応じて設定する方法を学ぶだけなので、本章を飛ばしても以降の理解に差し支えはない。本章では、新たな非同期の理論を導入することはない。

7.1 ランタイムの構築

「3章 非同期キューの自作」で、タスクを起動する関数を実装することで、非同期ランタイムでのタスクの動作を示した。この方法では、タスクの処理方法を詳細に制御することができた。一方、これまでに示したTokioのサンプルコードでは ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Async Rust

Async Rust

Maxwell Flitton, Caroline Morton
Rust Web Programming

Rust Web Programming

Maxwell Flitton

Publisher Resources

ISBN: 9784814401185Publisher Website