Synchronous VS Asynchronous Operations
One of the concepts that I recognized that lots of people get it wrong is the difference between synchronous and Asynchronous operations and before stating the difference between both of them lets get these points straight forward.
1-Sync and Async have nothing to do with your functions being being blocking or non blocking you could have a function that performs Asynchronous blocking operation like erasing a sector from the flash memory.
2-Sync and Async have nothing to with Multi-Threading Programing so performing Asynchronous operations is abstracted from the architectural design you adapt in your code you could perform Async operations in many design paradigm you could use.
Keeping the above concepts in your mind lets state the difference between Sync and Async operations.
Sync Operations is any type of operations a function will perform till the end result of the desired operation is obtained, for example a function that writes a buffer to the flash memory if the operation of writing data is a synchronous operation this function won't finish execution and return from its context till the whole buffer is written into the memory and the final result of the writing operation is returned immediately to the user after the functions has been called.
On the other hand Asynchronous Operations is any type of operations a function will start or perform that doesn't have to be finished after the function return from its context with a promise from the function to call back the calling context using a callback function after the whole operation desired result is finished.
using the example of writing a buffer into the flash if this operation is Async one so the writing of the flash will be divided into sub smaller operations the operation won't take place at one time so if the operation is writing 1KByte into flash it could divide the writing operations into 256 blocks "assuming this is the page size of the flash" and the functions would write 256Byte each time it is called till the whole buffer is written and then a callback for writing is finished should be asserted to inform the application that writing operation has finished. the function that performs the write operation could be called in a simple super loop architecture or from a scheduler or even from an interrupt where the processing of the operation could continue.
Now you know the difference between Async and Sync operations but you should ask yourself why would I need to use Async operations? the answer is that using Aync operations would shorten the time needed to perform long operations so your application could do other stuff at the same time without the headache of being stuck waiting for the whole operation to finish to continue executing your application.
Senior C++ Software Engineer | Adaptive Autosar Expert | Embedded Linux | Yocto | AOSP | CMake | GTest/Mock | Modern C++)
3 年Omar Ehab Do you have use case for Async blocking? Why do we need to use blocking with Async?