Thread の中止
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
[[Hirofumi Fujii Start Page]]
* Thread の中止 [#nd8ae04c]
Thread を外部から中止するために、
- POSIX thread の場合は pthread_cancel(pthread_t threadid)
- Windows thread の場合は TerminateThread(HANDLE threadha...
という関数が用意されている。
基本的には、この関数呼び出しで中止できるのであるが、
programming 上それほど単純ではない。
特に thread の停止は基本的には system 任せなので、いつ、...
明確ではない(POSIX thread では cancel を受け付ける位置を...
。従って、programming 上では、いつどこで止められても正し...
ように備えておかなければならない。
その thread が所有していた system 管理下の資源は(多分)s...
してくれる(べきである)。ところが、一般に thread にする...
間に共有資源があるからで、
(もし無ければ別 process で動かせばよい)ある thread を中...
この共有資源がどういう状態になっているかが一番の問題であ...
例を考えよう。
- ある thread (thread A とする)が装置からデータを読み出し...
- 別 thread (thread B とする)が詰め終わったデータを処理し...
- thread A が cancel された。
この時共有資源として、少なくとも
- buffer
- 詰め終わったデータ数
があるだろう。更にこの資源の排他制御と同期のために
- mutex
- semaphore
が使われるのが一般的である。
正常動作時には、thread A は
- mutex を lock
- buffer にデータを詰める
- 詰めたデータ数を設定する
- semaphore を increment
- mutex を unlock
一方 thread B は
- semaphore を待つ
- mutex を lock
- データを処理
- mutex を unlock
- semaphore を decrement
終了行:
[[Hirofumi Fujii Start Page]]
* Thread の中止 [#nd8ae04c]
Thread を外部から中止するために、
- POSIX thread の場合は pthread_cancel(pthread_t threadid)
- Windows thread の場合は TerminateThread(HANDLE threadha...
という関数が用意されている。
基本的には、この関数呼び出しで中止できるのであるが、
programming 上それほど単純ではない。
特に thread の停止は基本的には system 任せなので、いつ、...
明確ではない(POSIX thread では cancel を受け付ける位置を...
。従って、programming 上では、いつどこで止められても正し...
ように備えておかなければならない。
その thread が所有していた system 管理下の資源は(多分)s...
してくれる(べきである)。ところが、一般に thread にする...
間に共有資源があるからで、
(もし無ければ別 process で動かせばよい)ある thread を中...
この共有資源がどういう状態になっているかが一番の問題であ...
例を考えよう。
- ある thread (thread A とする)が装置からデータを読み出し...
- 別 thread (thread B とする)が詰め終わったデータを処理し...
- thread A が cancel された。
この時共有資源として、少なくとも
- buffer
- 詰め終わったデータ数
があるだろう。更にこの資源の排他制御と同期のために
- mutex
- semaphore
が使われるのが一般的である。
正常動作時には、thread A は
- mutex を lock
- buffer にデータを詰める
- 詰めたデータ数を設定する
- semaphore を increment
- mutex を unlock
一方 thread B は
- semaphore を待つ
- mutex を lock
- データを処理
- mutex を unlock
- semaphore を decrement
ページ名: