Synchronous asynchronous and blocking 2-Test small projects, synchronous asynchronous blocking 2-
In synchronous asynchronous and blocking 1, the advantages and disadvantages of synchronous asynchronous and non-blocking are described respectively, we use a small project to further explore their advantages and disadvantages.
1. Project Overview
Code: sync_io
Compilation: C ++, VS2010
There are two I/O items in the project. After you press the "Start" button, I/O will Start to be called according to different I/O processing methods. The processing function "OnStart () in the" Start "button () "The buttons will remain grayed out before they are returned.
2. IO Simulation
I/O is simulated by a simple Sleep () API randomly sleep for a period of time. This API will block the current thread. In fact, the current "WindowsMain/SyncIO. cpp ".
int CSyncIO::IO(){ DMFUNCTION; LARGE_INTEGER stCounter = {0}; ::QueryPerformanceCounter(&stCounter); srand(stCounter.LowPart); int nRandom= rand() % 100; for (int i = 1; i <= 100; i++) { ::Sleep(nRandom); } return nRandom;}
To make the test more visible, you can increase the value range of "int nRandom = rand () % 100;". The value range is 100 ms, and an IO operation can be completed for a maximum of 10 s.
3. to display different IO processing methods more clearly, this small project concentrates all UI operations into the "CMainWnd" class. To simplify code, when operating three different types of IO processing methods, you need to manually comment out or reverse comment here to select the corresponding processing class.
When you press the Start button, the OnStart () function of the IO processing class is called. If it is synchronous, the IO Result is displayed in the "IO Result" field. If it is asynchronous, the button is immediately changed to "Stop ", the Result is displayed only after I/O processing is complete.
When you press the Stop button, the OnStop () function of the IO processing class is called.
4. 3 classes with different IO Processing Methods
CSyncIO: Synchronous blocking mode that implements all basic functions (including interaction with the UI). The other two classes are inherited from this
CSyncIOByPolling: non-blocking synchronous mode. The OnStart () function is overloaded.
CAsyncIO: asynchronous non-blocking mode. The OnStart () and OnStop () functions are reloaded.