Synchronous asynchronous and blocking 4-synchronous non-blocking, synchronous asynchronous blocking 4-

Source: Internet
Author: User

Synchronous asynchronous and blocking 4-synchronous non-blocking, synchronous asynchronous blocking 4-

For project introduction and code, see synchronous asynchronous and blocking 2-Test small projects.

 

1. Implementation

Because IO is blocked, IO needs to be put into the thread for processing to implement polling IO results, and IO processing results should be returned to the exit code of the thread. Here, "CBaseThread" is used to encapsulate thread processing functions into classes.

unsigned CSyncIOByPolling::ThreadWork(){    return IO();}

 

In OnStart (), start two threads in sequence to process IO, and then round-robin. Once Any IO thread completes processing, the result will be sent.

In order not to fill up the CPU, a sleep interval is set for each round-robin, and the current progress of the UI is notified through the yyprogress () function.

bool CSyncIOByPolling::OnStart(){    int        nRetArray[] = {-1, -1};    HANDLE    hThreadArray[] = {NULL, NULL};    int        nThreadNum = sizeof(hThreadArray) / sizeof(hThreadArray[0]);    for (int i = 0; i < nThreadNum; i++)    {        hThreadArray[i] = StartThread();    }    int        nIndex = 0;    int        nCompletedNum = 0;    //polling get IO result    while (true)    {        for (int i = 0; i < nThreadNum; i++)        {            if (hThreadArray[i])            {                NotifyProgress(nIndex, i);                DWORD    dwExitCode = STILL_ACTIVE;                if(::GetExitCodeThread(hThreadArray[i], &dwExitCode))                {                    if (STILL_ACTIVE != dwExitCode)                    {                        nRetArray[i] = dwExitCode;                    }                    else                    {                        continue;                    }                }                ::CloseHandle(hThreadArray[i]);                hThreadArray[i] = NULL;                NotifyResult(nRetArray[i], i);                nCompletedNum++;            }        }        if (nCompletedNum >= nThreadNum)        {            break;        }        Sleep(TIMER_ELAPSE);        nIndex += TIMER_ELAPSE;    }    return    true;}

In simple terms, the non-blocking mode of synchronization is much more complex than the blocking mode of synchronization.

 

2. Test

Like synchronous blocking mode, the "Stop" button is always unavailable before OnStart () is returned, and the UI interface is also stuck (an old problem with synchronous mode ).

1> in non-blocking synchronous mode, although I/O results have no results, I can see the "Progress SS" that keeps moving forward (because I/O does not know when it will end, is actually the current time used), so that the user can know that although the UI is stuck, but at least the program is still not suspended, this is the benefit of doing more meaningful things without blocking.

2> If I/O does not provide timeout settings (such as I/O () in this example), you can also set a maximum polling time in the round robin to prevent OnStart () from never returning, as a result, the main thread (often the UI) cannot run normally.

3> multithreading is used in the polling mode. In this way, I/O concurrency is realized. In multiple I/O processes, the total time processed by all I/O tasks can be shortened.

In terms of user experience, the non-blocking mode of synchronization is better than the blocking mode of synchronization.

Of course, there is a problem with the polling interval. If the polling interval is too large, the CPU will consume a lot of polling code without too much meaning, for example, 1 s, but if the actual completion time of IO is only 10 ms, the actual completion time of IO will be extended to 1 s, which is worse than the synchronization blocking mode.

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.