2

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?

  • 3
    If you read the code in the example, one of the functions is calling `CreateThread()`, and passing the address of one of the other functions. That is the windows API way of creating a thread that will execute the passed function. So, yes, it is multithreaded. – Peter Oct 07 '19 at 09:38
  • @Peter Is it TRUE that asynchronous functions can only be realized by Multithreading? –  Oct 07 '19 at 09:44
  • No, it's not true. However, what you want to know is rather a yes, it's true. Please take some time to read the definition of multithreading, much of what you want to know follows from that definition. – Ulrich Eckhardt Oct 07 '19 at 09:54
  • No, it is not. Multithreading is only one way. It is possible to have asynchronous operations within a single-threaded program. Have a look at https://stackoverflow.com/questions/34680985/what-is-the-difference-between-asynchronous-programming-and-multithreading – Peter Oct 07 '19 at 09:54
  • It is not entirely true, it is technically possible to ask a thread to do something else when it is not busy executing code and is waiting for the OS to complete a task. QueueUserAPC() is the core winapi function to accomplish this. Quite an advanced programming technique, does not get used very often and unrelated to the linked article. – Hans Passant Oct 07 '19 at 09:54
  • yes, can, but only from place where you do alertable wait or call `NtTestAlert` (https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-queueuserapc). and multithreading not mandatory need ( [There Is No Thread](https://blog.stephencleary.com/2013/11/there-is-no-thread.html) ) – RbMm Oct 07 '19 at 10:24

1 Answers1

1

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.

Robin Hsu
  • 174
  • 1
  • 9