Skip to content
Back to skills

Thread Delay

ASecurity

Avoid overusing threadDelay because time-dependent code is unstable and unportable. Prefer synchronization variables like TMVar or retry combinators. Use when writing or reviewing Haskell code that delays threads, waits, or polls.

  • 3 stars
  • 0 votes
  • 0 copies
  • 5 views
  • Added May 27, 2026
data-ai

Security analysis

A100/100

Scanned May 27, 2026

npx -y skills add ncaq/konoka --skill thread-delay --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Thread Delay?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Thread Delay
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ncaq-thread-delay/badge)](https://www.skillsdirectory.com/skills/ncaq-thread-delay)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: thread-delay
description: Avoid overusing threadDelay because time-dependent code is unstable and unportable. Prefer synchronization variables like TMVar or retry combinators. Use when writing or reviewing Haskell code that delays threads, waits, or polls.
user-invocable: false
---

# `threadDelay`

## 乱用を避ける

`threadDelay`はスレッドを指定した時間だけ遅延させる関数です。

`threadDelay`を乱用するのはやめましょう。

時間に依存するコードは安定性や移植性が低いためです。

次の実行でも同じ時間で処理が完了するとは限りません。
他のマシンでは同じ時間で処理が完了しないかもしれません。

また必然的にその時間だけ実行がストップしてしまうので、
待つように命令した分だけ実行が遅くなります。

## 代替手段

別のスレッドの実行を待つ必要がある場合などは、
`TMVar`などの同期変数を使って正確に同期するのが望ましいです。

言語外部のデータやイベントに依存する場合は、
[retry: Retry combinators for monadic actions that may fail](https://hackage.haskell.org/package/retry)
パッケージの`exponentialBackoff`などを使って、
短い単位の繰り返しの待機をしてください。

固定の待機をするよりは実行のストップが短くなることが見込めます。
また最大待機時間の値を固定値より大きくすることが許容できるため、
実行の安定も見込めます。

## 永久的な停止の意図

以下のように意図的に永久的にスレッドを停止する場合は問題ありません。

```haskell
forever $ threadDelay maxBound
```

そのスレッドが持つリソースを、
GCに回収されないように保持するために、
こういった永久的なsleepが必要な時もあります。

## テストコードでの利用

テストコードを書く時に、
ロジックに実行を待つ部分が実装されていない場合などは、
同期変数を組み込むとロジックを複雑にしてしまうことがあるので仕方ない時もあります。

テストコードなら壊れても致命的ではないので許容することもありますが、
やはり他の方法を使えないか検討するべきです。

とりあえずretry系統を使えばだいたいの場合は`threadDelay`を使うよりはマシになるでしょう。

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.