windows - Question about Win32 ##Event## synchronization object -


first let me introduce application scene:

i have service application spying status of something, while have multiple applications waiting status change. once status changed, each application read status value (via named filemap object) , execute corresponding actions, , wait status changed again.

so used named event object synchronization work. applications waiting event signaled, , service application set event signaled when status changed.

i need guarantee when status changed, each waiting application released , is released once!

i have tried these 2 methods

method 1

  1. create manual reset event;
  2. when status changed, first call setevent, call resetevent immediately.

method 2

  1. create manual reset event;
  2. when status changed, call pulseevent.

both methods seem work during test. think neither of them reliable because:

for ## method 1 ##, maybe of waiting threads won't chance executed before resetevent function called.

for ## method 2 ##, microsoft has claimed pulseevent unreliable , should not used.

is there workable solution case? advice welcome.

there no way can safely implemented o(1) synchronization primitives.

to inform n applications new status change use n events. service should set them all, , every application should reset corresponding event upon handling current status change.

to wait until applications handle new status change service can use set of n events , waitformultipleobjects bwaitall==true. each application should set corresponding event.

so, service loop: observe status change, write shared memory, set events, wait on b events, reset b events, continue loop. every application loop: wait on a(i) event, reset a(i), handle status change, set b(i), continue loop.

both , b events can of auto-resetting type. don't have reset anything.

if feel green , don't want waste resources can use sort of reversed semaphore instead of b set of events. can implemented 1 shared counter synchronized mutex , 1 event (b') notify service. instead of waiting on whole b set service waits on b' , when wait on set counter n. instead of setting b(i) event each application should decrement counter, , if counter drops 0 last application should set b'.

you can't bypass set of events. problem not in setting event in resetting. resetting a(i) event application not miss status change. , not helpful use semaphore here.

note solution not take account possible crash of application. if happens service wait forever non-existing application respond.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -