Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

Multithreading in C++ - Handles are not closing?

I have a multithreaded program in C++ for winsock. It creates a new thread for each connection.

The problem is, each time it creates a new multithread, it creates a handle. Now, when the client disconnects and I close the thread, the handle still exists in memory and it is eating me up. How can I close the handle as well? Here is my multithreaded function http://pastebin.com/L0hB7AK2

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    EDIT:

    In my earnest to state the unnecessary use of printf(), I forgot to add that you can close a thread handle with the CloseHandle() function. (Look at the example code included in the CreateThread() function reference in the Windows SDK.)

    Also, you should read the ExitThread() function documentation in the Windows SDK because there are differences in how ExitThread() should be used if you ever use C++ objects within a thread.

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

    Ooh, you weren't kidding when you said that the code was messy. I mean, your thread procedure has to be completely recoded.

    Well, the first thing I noticed was that you were using printf() in your thread. Hopefully, you linked in the multi-threaded version of the C++ run-time code, or used static run-time linking. This link explains a little about calling standard C functions within threads:

    http://msdn.microsoft.com/en-us/library/1bh5ewb2%2...

    You can get more information if you read the CreateThread() and ExitThread() function reference information in the Windows Platform SDK. You're supposed to use _beginthreadex() and _endthreadex() instead of CreateThread() and ExitThread().

    Next, your thread procedure can just return 0 or some other value and then pass the return value to code outside of receive_cmds(), instead of calling ExitThread() from within the thread. It is necessary in some cases, so getting into the habit of stopping/terminating a thread outside of the thread might be a good idea.

    Next, your 'res' variable is allocated and initialized to 0 at the start of the the procedure, but the value is never changed and then your second 'IF' statement checks it's value.

    Last, you don't even need to bother with any of the standard C functions because there's nothing there that you need. You can just remove the C/C++ startup code entirely if you don't use the standard C/C++ functions or use exceptions. If you want to write console text, just use the functions that Windows provides.

    Look at this:

    #include <windows.h>

    void __cdecl Startup(void);

    unsigned char hello[]="Hello world!";

    DWORD numwritten;

    __declspec (naked) void Startup(void)

    {

    WriteConsoleA (GetStdHandle( STD_OUTPUT_HANDLE), &hello,12, &numwritten, 0);

    ExitProcess(0);

    }

    The 'Hello World' program code above is only 37 BYTES long (not including the import address jump table). That's a very small fraction of the printf() function's code size itself. The startup code and MSVCRT.DLL file are not used and the program just needs Kernel32.dll.

  • ?
    Lv 4
    5 years ago

    do no longer sweat the small stuff!!!! particularly once you write like this! I consider OW, and my prominent continues to be..... yet one escaped. It ran from his eye to his chin touching each thing in between. the 1st set the direction for the rest because of the fact the armada of emotion left his face and soaked his sleeve. Now do i'm getting a hug for answering it two times??????

Still have questions? Get your answers by asking now.