Thread: Closing thread handles from within

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Closing thread handles from within

    When a thread is created, a handle is returned which should be closed when its not used anymore.

    Can that handle be closed from within the thread function?

    My C++ thread class requires this to be possible, and I also think that it is legal and can be done.



    Code:
    DWORD WINAPI Petter::Thread::threadProc( void* lpData )
    {
    	//Get the startup data for this thread
    	ThreadStartupData* threadData = reinterpret_cast<ThreadStartupData*>( lpData );
    
    	//Obtain a pointer to the thread
    	Thread* thread = threadData->thisPtr;
    
    	//Save the handle in the thread object
    	thread->threadHandle = threadData->threadHandle;
    
    	//Run the thread's main function
    	thread->run();
    
    	//Close the thread's handle
    	//WARNING: Can this be done inside the thread?
    	CloseHandle(threadData->threadHandle);
    
    
    	//Delete the thread object
    	delete thread;
    	delete threadData;
    
    	return 0;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, once you call CloseHandle(), there's no guarantee that those deletions at the end of the function will occur. That's the real problem. But I see what you're getting at - a scopeless thread that deletes itself! Very cool.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, I'm not so clear on the two separate data structures (ThreadStartupData and Thread), it looks as if the latter is the base class for the former, but they refer to the same object? If so, why delete both? Anyway my idea was to call CloseHandle() from the last possible point of execution - the destructor of the Thread object. That way, deleting the object would close the handle.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    From MSDN:
    Closing a thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread.
    As a result, allowing a thread to close its own handle may not necessarily remove the thread from the system since the thread has not terminated.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm...doesn't sound good.

    Not much of a solution really:

    1) Don't close the handle. It will be closed when the application exits.

    Or if you want to be a good samaritan about it:

    2) Create a thread manager class that create's all thread objects. Handles are stored in a linked list. Call's to ThreadManager::collect() iterates through the list and closes all handles that are not active. ThreadManager can even run the collection duties in a separate thread to ensure the largest possible 'thread handle cache' from the system at all times.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This is getting over complicated. If you don't need the thread handle simple close it straight after the call to CreateThread:
    Code:
    hThread = CreateThread(...);
    CloseHandle(hThread);
    If you only need the handle from within the thread do this and use the GetCurrentThread function. You can close the handle from within the thread but as mentioned this will not terminate the thread. If you want to terminate your thread before the ThreadProc function returns use ExitThread().

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    OK, it is legal to close the thread handle from within the thread.
    As you can see, I don't want the thread to terminate when I close the handle (the deletes after must be executed)

    I now have a nice thread library that I will post here later.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared memory implementation using thread
    By kumars in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:24 AM
  2. user thread library
    By Eran in forum C Programming
    Replies: 4
    Last Post: 06-17-2008, 01:44 AM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  4. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM
  5. Replies: 12
    Last Post: 05-17-2003, 05:58 AM