> For the complete documentation index, see [llms.txt](https://help.diarkis.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.diarkis.io/diarkis-client/diarkis-module/diarkis-nosureddo.md).

# Diarkis のスレッド

本ページでは Diarkis で使用するスレッドについて説明します。 Diarkis Module のスレッドは、`DiarkisInterfaceBase::DiarkisInit()` の初期化の中で 作成 されます。`IDiarkisTransport::Connect()` を呼び出した際に UDP/TCP/P2P のスレッドが 作成 されます。接続する Diaskis サーバに合わせて ランタイムライブラリのスレッドが増えます。

* ランタイム・ライブラリ
  * UDP サーバー接続時は、以下の２つが作成されます。
    * Send Thread : Send Pedding Buffer に溜まったパケットを送信するためのスレッドです。
    * Receive Thread : Socket で受信したパケットから Event Scheduler に Event を push するためのスレッドです。
  * TCP サーバー接続時は、以下の１つが作成されます。
    * Network Thread : パケットの Send と Receive するための スレッドです。
  * P2P 接続時は、接続相手毎に以下が作成されます。
    * Holepunch Thread : ホールパンチする際にするために一時的に作成されるスレッドです。
* Diarkis Module
  * Runtime Thread : Diarkis サーバからの 応答 / 通知イベントを呼び出すためのスレッドです。
  * Logger Backend Thread : Diarkis のログ・バッファリングして処理するためのスレッドです。

### UDP 接続時の スレッドのシーケンス図

{% @mermaid/diagram content="sequenceDiagram
box Application

```
participant App Thread
participant Runtime Thread
```

end
box Library
participant Pending Buffer
participant Event Scheduler
participant Send Thread
participant Receive Thread
end
box Diarkis Server
participant Server
end

```
Note over Receive Thread: Socket を Max 100 ms<br/>開いて待機
Note over Send Thread: Max 100ms 待機<br/>Pending Buffer に<br/>Push された際に<br/>Thread が起こされる
Note over Runtime Thread: Max 100ms 待機 or<br/>Event Scheduler に<br/>Push された際に<br/>Thread が起こされる
```

　
Activate App Thread
App Thread->>Pending Buffer: Send()/RSend() Pending Buffer<br/> にメッセージ Push
Activate Pending Buffer
loop Send Loop
Pending Buffer->>Send Thread: Thread を Wait から起こす
Activate Send Thread
Send Thread->>Pending Buffer: メッセージチェック・取得して処理継続
Deactivate Pending Buffer
Send Thread->>Server: Send
Activate Server
Deactivate Send Thread
end

loop Receive Loop
Server->>Receive Thread: Recv
Deactivate Server
Activate Receive Thread
Receive Thread->>Event Scheduler: Event Scheduler にイベント Push
Activate Event Scheduler
Deactivate Receive Thread
Event Scheduler->>Runtime Thread: Thread を Wait から起こす
Activate Runtime Thread
end
loop Runtime Loop
Runtime Thread->>Event Scheduler: Event チェック・取得して処理継続
Deactivate Event Scheduler
Runtime Thread->>App Thread:コールバック
Deactivate Runtime Thread
end

　　　　Send Thread->>Pending Buffer: メッセージチェック<br/>PendingBuffer が空なら何もせず Wait。
Activate Send Thread
　　　　Note over Send Thread: Max 100ms間隔
　　　Deactivate Send Thread
Activate Receive Thread
　　　　Receive Thread->>Receive Thread: Socket にメッセージを受信しない場合でも、<br/>異常や終了処理チェックのため<br/>Max100ms に1回ループを回す
　　　Deactivate Receive Thread

　　　Runtime Thread->>Event Scheduler: Eventチェック<br/>Event が無せず Wait
Activate Runtime Thread
　　　　Note over Runtime Thread: Max 100ms間隔
　　　Deactivate Runtime Thread
Deactivate App Thread

" %}

#### 必要に応じて Diakis サーバーの複数用意する場合

例えば、マッチング用と TURN 用の `UDP` サーバを用意する場合は、`DiarkisInterfaceBase` のインスタンスをサーバー分の2つ用意する必要がありますので、ランタイム・ライブラリのスレッドは （`Send Thread` と `Receive Thread` ）ｘ 2 の 計 4 つのスレッドが作成されることになります。

上記の場合でも Diarkis Module の スレッド （`Runtime Thread` と `Logger Backend`）は1つずつに作成されるだけなので、計 マッチング用 x 2 TURN 用 x2 Diarkis Module x2 の 6つになり、 P2P 接続する際は接続相手毎に一時的にスレッドが作成されます。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.diarkis.io/diarkis-client/diarkis-module/diarkis-nosureddo.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
