Click to See Complete Forum and Search --> : Polling versus interrupt/notification


Bore
May 4th, 2003, 05:54 PM
We are having a debate in one of our engineering groups over whether to use a polling model or a notification model in part of the code. I've been searching newsgroups, compsci journals and this forum for a while but haven't found any general discussion of how to choose between polling or notification. My first question is whether anyone knows of a good discussion of what the issues are.

In case anyone has some of his own input, here are some details. The UI has dropdown menus in it. The dropdown menus may look different depending on the state of the app. The simplest example is in the "Edit" menu. If the state allows the user to use the "Cut" command, then "Cut" should be enabled. Otherwise, "Cut" should be disabled. Simple enough.

It seems that the easiest option, that of having the menu itself analyze app state and take care of itself, is not feasible. Getting at certain elements of the app state is just too slow, and it makes menu dropdown sluggish.

An alternative that everyone seems to like is to have some other part of the code analyze the app state and store the results in a central object, such that a menu dropping down can go look there and take care of itself quickly. The problem then arises as to the model to use for keeping that central object up-to-date.

Some in the group want to have Windows send us a periodic message, ON_IDLE, telling us that nothing else is going on. At this point, a function would run to analyze the app state and update the central object. This is what we're calling the "polling" model, or "pulling".

Others in the group want to have the parts of the code that may affect relevant app state call into the central object at the instant that the state is changed. This is what we're calling the "notification" model, or "pushing".

Those in favor of polling say that Windows is providing this service for us, so why not use it? Also that it centralizes the code that has to perform the state analysis.

Critics of polling say that it is a model to be avoided except when it's absolutely necessary. Also that the function that performs the state analysis becomes a dumping ground for all sorts of poorly thought-out processing.

Those in favor of notification say that it's a cleaner model that enforces much-needed discipline on those writing the code that changes app state.

Critics of notification say that it's too complicated to implement and requires programmers to always think about updating state.

If anyone knows of any good discussions of the issues, please let me know. If you have any opinions, feel free to share.

lord loh
May 5th, 2003, 12:46 AM
Technical Talk....I am an amature...but here is my input...It may have been thought of and even mentioned in the previous post(Which I admit I did not understand 100%)

If the app could set state flags on change of state, change of focus or some valid event....The menu will check these flags before enabling or disabeling itself...

(Is appears similar to polling or pulling)

Do tell me what you say....

Thank you.

Linenoise
May 6th, 2003, 06:57 PM
We've run into this here, and have converted the code over from a polling to event-driven architecture.

We found that polling had one downside which made life difficult for us - because it's nondeterministic when it runs, it's quite possible to miss state changes. Consider an app that can be in state A, B, or C. Now, let's say that the transition A -> C is invalid but any other transition is ok. The app runs, and goes from A -> B -> C. However, because you are polling, there's a chance that the menu code will see A -> C, and fail. With an event driven system, you're guaranteed to get all of the events.

In my opinion, it really comes down to two things:

1) Polling tends to cause you to not store your own data - you read the current state of the system directly from another object.

2) Polling works fine if you only care about the current state. If you care about the transitions, then event driven is easier to work with.

Whatever you do, be careful mixing them. If you react to an event, and poll to get more data, you wind up with almost a preemption problem - the data you're polling might already be in your event queue, and you shouldn't be reacting to it yet.

KingTermite
May 9th, 2003, 11:18 AM
That issue comes up often at my work too (a lot of embedded architectures).

It usually boils down to a couple of major considerations.

Just HOW QUICKLY and/or IMPORTANT to know whatever state change you are wanting to see?

If you are considering polling....how possible is it to get into starvation situations?

If event driven and using interrupts....make sure your design considers the case of starvation of servicing something because of constantly getting higher priority interrupts/events.