Synchronous asynchronous and blocking 2-Test small projects, synchronous asynchronous blocking 2-

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.