문제

런타임에서 스레드가 기다려야하는 런타임에서 안전하게 변경하는 방법을 궁금합니다.

이벤트를 통해 동기화 된 두 개의 스레드 (A 및 C)가 두 개의 스레드 (A 및 C)가 있다고 가정합니다. A는 그 일을 주기적으로하고 C가 그 일을 시작할 수있는 알림을받을 때까지 대기합니다 (예 : AutoRoSetEvent). 패턴은 A-C-A-C ...

나중에 새 스레드 (예 : 사용자 작업)가 시작 되고이 방식으로 두 개의 기존 스레드 사이에서 작업을 실행해야합니다. A 작업을 수행 한 다음 B를 생성 한 다음 IT 신호가 신호를 완료합니다. . 이제 패턴은 abcabc ...

스레드 C가 A 이상으로 공유 된 EventWaithandle에서 기다리고 있기 전에 B가 B와 공유되는 다른 EventWaithandle에서 C를 기다리는 안전 메커니즘이 있어야합니다. 까다로운 부분이 C에서 사용 된 이벤트 와이션을 대체하는 것처럼 보입니다. 일단이 작업이 완료되면 eventWaitHandle을 사용하여 작업 및 C 작업에 대한 신호를 보내기 위해 eventWaithandle을 사용할 수있는 B를 쉽게 시작할 수 있어야합니다. 메커니즘은 스레드 B를 안전하게 마운트 해제하고 스레드 A와 C 만 작동하는 초기 상황으로 돌아가는 방법을 제공해야합니다.

이벤트를 통해이를 수행하는 안전한 방법이 있습니까? 그렇지 않은 경우 다른 제안이 감사 할 것입니다.

도움이 되었습니까?

해결책

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