_beginthreadex () and asynchronous thread synchronization issues

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_33810188/article/details/91816237

CRT has a very good multi-thread function _beginthreadex (), you can design synchronous and asynchronous programs run by this function. As follows, after the changes in someone else's program, you can be more in-depth understanding of the problem of synchronous and asynchronous thread!

---------------

#include <iostream>
#include <windows.h>
#include <process.h>

CRITICAL_SECTION g_cs; // critical section lock

struct _STRUCT_DATA_ typedef
{
    int ID; // for identifying the ticket ID
    int tickets;

}_DATA, *_pDATA;

unsigned __stdcall Fun1Proc(LPVOID lpParam);
unsigned __stdcall Fun2Proc(LPVOID lpParam);


int main()
{
    HANDLE hThread[2] = { NULL,NULL };
    unsigned threadid[2] = { 0 };

    _DATA stru_data;
    stru_data.id = 0;
    stru_data.tickets = 1000;

    InitializeCriticalSection (& g_cs);
    hThread [0] = (HANDLE) the _beginthreadex (NULL, 0, Fun1Proc, & stru_data, 0, & ThreadID [0]); // child threads. 1
    hThread [. 1] = (HANDLE) the _beginthreadex (NULL, 0, Fun2Proc, & stru_data, 0, & threadid [1]); // child thread 2
    Sleep (100); // child threads running for some time it

1 #if
    / * Because it is an asynchronous architecture, the main thread continues to run! * /

    while (TRUE) // for asynchronous problem, to obtain health thread through GetExitCodeThread, if not finished running, the delay allows the child thread has finished running, otherwise the main thread continues to wait!
    {
        DWORD dwExitCode1, dwExitCode2;
        GetExitCodeThread (hThread [0], & dwExitCode1); // obtain the thread is finished running, if the run is completed return dwExitCode = 0, otherwise 259
        GetExitCodeThread (hThread [1], & dwExitCode2);
        IF (dwExitCode1 =! ! 0 || dwExitCode2 = 0) // one has not finished running, waiting 100ms
        {
            Sleep (100);
        }
        the else
        {
            BREAK;
        }
    }
#endif // through the while loop, can reach the main thread and a sub thread synchronization!
    LeaveCriticalSection (& g_cs);

    std::cin.get();
    return 0;
}

unsigned __stdcall Fun1Proc(LPVOID lpParam)
{
    _pDATA data = (_pDATA)lpParam;

    while (TRUE)
    {
        EnterCriticalSection(&g_cs);

        IF (DATA-> tickets> 0)
        {
            Sleep (. 1);
            STD :: COUT << "ID:" << DATA-> ID :: STD ++ << endl;
            STD :: COUT << "Sell Thread1 Ticket:" DATA- <<> :: STD tickets-- << endl;
            LeaveCriticalSection (& g_cs);
        }
        the else
        {
            LeaveCriticalSection (& g_cs);
            BREAK;
        }
        // Sleep (. 1); // add a time delay, the test for shared data the visits
    }

    return 0;
}

unsigned __stdcall Fun2Proc(LPVOID lpParam)
{
    _pDATA data = (_pDATA)lpParam;

    while (TRUE)
    {
        EnterCriticalSection(&g_cs);

        if (data->tickets > 0)
        {
            Sleep(1);
            std::cout << "id: " << data->id++ << std::endl;
            std::cout << "thread2 sell ticket: " << data->tickets-- << std::endl;
            LeaveCriticalSection(&g_cs);
        }
        else
        {
            LeaveCriticalSection(&g_cs);
            break;
        }

    }
    return 0;
}

---------------

Above, by #if 0 ... #endif, asynchronous design program can be found in the asynchronous condition, the main thread will run regardless of the condition of the child thread, the main thread will then run, leading after the last main thread has finished running, child the threads have not finished running! as the picture shows:

You can pair by #if 1 ... #endif running thread to monitor if the child thread has not finished running, the main thread to wait for a certain time, if the child thread has not finished running, continue to wait for some time, so from the angle of the main thread code and the sub-synchronous control thread, the purpose of blocking, of course, function in other threads, the threads have a special thread blocking and separation functions. as the picture shows:

It should be noted, adding critical sections in the program is to ensure that each thread when accessing public data channeling data will not achieve the purpose of interlocking through the critical area to ensure the security of data, of course mutex function can also achieve the same purpose.

 

Guess you like

Origin blog.csdn.net/qq_33810188/article/details/91816237