質問

スレッドが待つべきであるEventWaithAndleを実行時に安全に変更する方法を疑問に思います。

例えばEventWaithAndLESLSを介して同期されている2つのスレッド(AとC)があるとします。 Aは、そのジョブを周期的に、Cがそのジョブを開始できるように通知を受け取るまで待機します(例えばAutoResetEventだけ)。パターンはA-C-A-C ... です

新しいスレッド(B)の後に(例えば、ユーザアクションによって)起動され、そのジョブはこのように2つの既存のスレッド間で実行されるべきである.aはそのジョブを作り、次に信号BおよびBが信号Cを終了する。 。今パターンはabcabc ...

スレッドCがA・後で共有するEventWaithAndleを待っていた前に、Bで共有されている別のEventWaithandleでCを待っている安全なメカニズムがあるはずです。これが行われると、私は簡単にBを起動できるはずだ、それはJobを待つためにジョブを待つためにCoveSleanとCジョブを送信するためのEventWaithandleを使用することができるはずです。このメカニズムは、スレッドBを安全にアンマウントし、スレッドAとCのみが機能している初期の状況に戻る方法も提供する必要があります。

EventWaithandleでこれを達成するための安全な方法はありますか?そうでなければ、他のすべての提案が理解されるでしょう。

役に立ちましたか?

解決

If task A knows about the change, then have task C own the event. Task A signals task C's event if C is to be next, or task B's event if task B is to be next.

Alternatively, use the same mechanism as for changing any other shared data: acquire a mutex across all accesses to the handle. e.g. task C acquires the lock, reads the handle, releases the lock, waits on the handle. To change it you have the UI thread acquire the lock, change the handle, release the lock.

他のヒント

Have you thought about implementing some sort of scheduler where the threads can register (deregister) with a handle to start and to signal completion. And the scheduler then takes care of starting the next thread by setting the appropriate start-event and continues with the next thread, when the previous one has set the completion event.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top