Alexey Romanov
Alexey Romanov

Reputation: 170919

Notify and stop gen_event manager atomically

When a particular message is received by my gen_event manager process, I want it to stop after all handlers have handled it and before they get and handle any other events. The only way I could find is this:

-module(manager).

...

stop(Reason) ->
    gen_event:sync_notify(manager, {stop, Reason}),
    gen_event:stop(manager).

But this requires all handlers to return remove_handler from handle_event({stop, Reason}, State), otherwise they could handle an event sent from a different process after sync_notify and before stop. I would prefer to have an approach that imposes no requirements on handlers.

Upvotes: 3

Views: 158

Answers (1)

I GIVE TERRIBLE ADVICE
I GIVE TERRIBLE ADVICE

Reputation: 9648

As far as I know, there is no other way to do it than the one you're using for handling in a way that is truly limited to one call, outside of just plainly killing the event manager with exit(Pid, Reason) or ordering it to be shut down by its own supervisor.

Upvotes: 2

Related Questions