views:

37

answers:

2

When an Asynchronous Procedure Call (APC) occurs it is executed 'Asynchronously' to the current context of the thread. Per this MSDN info: APC

Now my question is, what exactly does it mean 'execute asynchronously to the context of the current thread'? Is it that executes besides what the thread is already executing, or is the thread interrupted to execute the APC first and then continue its work?

Because to my knowledge a processor can not 'really' do two things at once. Unless I've completely misunderstood the 'asynchronous' concept here.

Can anyone offer an explanation or a link to an explanation?

A: 

This is a much more general question. How do you think a computer handles multi-tasking if it couldn't do many things at once? It's true that at any given instant, it might only be doing one thing, but each task (be it running a web browser or executing your APC thread) is time-sliced and executed concurrently on the processor. They appear to be executing at the same time, although they're actually interleaved on the processor.

Of course, if you have multiple cores, as most machines do now, they genuinely can execute many things at once.

skaffman
OK, so the APC is concurrently executed, if we're talking about a uniprocessor system, using the time slice mechanism?
Tony
+2  A: 

A thread must be in an alertable state to run a user-mode APC.

When a user-mode APC is queued, the thread to which it is queued is not directed to call the APC function unless it is in an alertable state.

A thread enters an alertable state when it calls the SleepEx, SignalObjectAndWait, MsgWaitForMultipleObjectsEx, WaitForMultipleObjectsEx, or WaitForSingleObjectEx function. If the wait is satisfied before the APC is queued, the thread is no longer in an alertable wait state so the APC function will not be executed. However, the APC is still queued, so the APC function will be executed when the thread calls another alertable wait function.

execute asynchronously to the context of the current thread means the APC function will be executed when the thread calls alertable wait function and switch to the alertable state.

I recommand you read

Windows via C/C++, Fifth Edition 
Chapter 10 - Synchronous and Asynchronous Device I/O 
whunmr
Thanks ! Makes sense!! :)
Tony