简体   繁体   中英

one thread to exit them all

I have a main program that generates a few threads (using a while loop with accept() to get clients), and one that all it has to do is "listen to the keyboard" and when the user enters the word exit it will close the entire program. first, the main program create the listening thread, then it enters a while loop that accept the clients. even if the listening thread get the exit input the loop is still stuck on accept.

i don't have to use a seperate thread to listen to the keyboard but i could'nt find a none blocking way that would work.

the listening thread:

DWORD WINAPI ListenService(LPVOID lpParam)
{
    char buffer[5];
    if (EOF == scanf("%s", buffer))
    {
        printf("faile get word from keyboard\n");
    }
    if (buffer[4] != '\0')
        strcat(buffer, "\0");
    if (STRINGS_ARE_EQUAL(buffer, "exit"))
    {
        return 999;
    }
    return -1;
}

in the main code:

ThreadListen = CreateThread(NULL,0,ListenService,NULL,0,&(ThreadId));   
while(1)
{
    SOCKET AcceptSocket = accept(MainSocket, NULL, NULL);
    if (AcceptSocket == INVALID_SOCKET)
    {
        printf("Accepting connection with client failed, error %ld\n", WSAGetLastError());
        CleanupWorkerThreads();
        WSACleanup();
    }
    printf("Client Connected.\n");
}

There are many different ways you can handle this.

You can abort a blocked accept() by simply closing the listening socket.

Or, you can use select() with a short timeout to detect when a new client is waiting before then calling accept() . You can check your exit condition in between calls to select() . Just be aware that there is a small race condition where a client may disconnect between the time select() and accect() are called, so accept() may still block, if there are no more clients waiting.

Or, you can get rid of your threads and just use non-blocking sockets in a single thread, checking your exit condition periodically in between socket operations.

Or, you can use asynchronous sockets, using WSACreateEvent() , WSAEventSelect() , and WSAWaitForMultipleEvents() to detect socket activity. Then you can create an addition event to wait on for when the exit condition happens.

Or, you can use an I/O Completion Port to handle socket activity, and then you can post a custom exit packet into the IOCP queue using PostQueuedCompletionStatus() to "wake up" any waiting threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM