user11847100
user11847100

Reputation:

Win32 Can functions asynchronously running on the same thread?

I wonder whether win32 functions can asynchronously running on the same thread.

I saw an example of the asynchronous function in MSDN

https://learn.microsoft.com/en-us/windows/win32/wsw/asyncmodelexample

Is this involving multithreading?

Edit:

Is it TRUE that asynchronous functions can only be realized by Multithreading?

Upvotes: 2

Views: 1323

Answers (1)

Robin Hsu
Robin Hsu

Reputation: 308

I wonder whether win32 functions can asynchronously run on the same thread.

No. win32 functions serve system calls. They run in their own context (or, you could think them as system threads). They may block your thread (synchronous system calls) or not block your thread (asynchronous calls). For those synchronous calls, it appears like running on your thread (blocked), but in fact, it runs in a different context.

I saw an example of the asynchronous function in MSDN https://learn.microsoft.com/en-us/windows/win32/wsw/asyncmodelexample Is this involving multi-threading?

Yes. This example implements multi-threading (two threads). One of the thread uses asynchronous notification (callback function) to notify the other thread for the results.

Is it TRUE that asynchronous functions can only be realized by Multithreading?

No. Like previously mentioned, some system functions are asynchronous (or with both synchronous and asynchronous options). You can use those functions to do asynchronous operations. It's like that the system provides multi-thread for you, without the need to implement a multi-thread program yourself.

Note that the system calls do only system services, a well-defined set of system functions. If you need to achieve something other than just system services in your other thread, then, yes, you need another thread to do it.

Upvotes: 2

Related Questions