0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

スレッド優先度の逆転を一時的に回避する方法(Swift)

0
Posted at

はじめに

本記事は どすこい塾 Advent Calendar 2025 の11日目の記事です。
昨日も @uhooiXCFrameworkをSPM経由で導入する方法 でした。

スレッド優先度が逆転している実行時エラーが発生したとき、スレッド優先度を一時的に下げて回避する方法を紹介します。

環境

  • OS:macOS Sequoia 15.6(24G84)
  • Swift:6.2.1

スレッド優先度逆転の実行時エラー

以下のような実行時エラーが発生したことはありませんか?

Thread running at QOS_CLASS_USER_INTERACTIVE waiting on a thread without a QoS class specified. Investigate ways to avoid priority inversions

(`QOS_CLASS_USER_INTERACTIVE` で実行中のスレッドが、QoSクラスが指定されていないスレッドを待機している。優先度の逆転を回避する方法を調査してください)

QoS(Quality of Service)クラスの優先度が逆転しているため、回避してくださいというエラーです。
今回のケースでは、QoSが指定されていない場合は QOS_CLASS_DEFAULT 扱いとなり、 QOS_CLASS_USER_INTERACTIVE より優先度が低いためエラーとなっています。

スレッド優先度が逆転しないよう修正できればいいですが、難しい場合もあります。
その場合、現在のスレッド優先度を一時的に下げて対象の処理を実行し、そのあと元に戻すことで回避できます。

ワークアラウンド

例えば unityFramework.runEmbedded() の実行時にエラーが発生しているとします。
実行前に Thread.current.threadPriority0.5QOS_CLASS_DEFAULT 相当)へ変更し、実行後に元に戻すことで回避できます。

+ // スレッド優先度が逆転するのを避けるため、一時的に下げる
+ let originalPriority = Thread.current.threadPriority
+ Thread.current.threadPriority = 0.5

unityFramework.runEmbedded(withArgc: CommandLine.argc, argv: CommandLine.unsafeArgv, appLaunchOpts: nil)

+ // 元のスレッド優先度に戻す
+ Thread.current.threadPriority = originalPriority

おわりに

単純な回避策ですが、場合によっては有用です。
みなさんもどうしてもスレッド優先度の逆転が修正できない場合に使ってみてください。

以上 どすこい塾 Advent Calendar 2025 の11日目の記事でした。
明日は未定です。

参考リンク

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?