I have a bunch of threads created with CreateThread(). When each thread
is exiting, it posts a message saying that it's about to quit. I'd like
to do the CloseHandle() on the thread handle in that message handler.
But, how can I tell which handle is the one for that particular thread
which just finished? Is there a call like GetThreadHandle() I can use
from within the thread to pass back the same handle I got from
CreateThread() to the message-handler?

Thanks,
PaulH

std::vector<HANDLE> m_vThreads;

CMyDlg::StartAThread()
{
THREAD_INFO *pTI = new THREAD_INFO
/*...*/
m_vThreads.push_back(AtlCreateThread(MyThread, pTI);
}

static DWORD __stdcall MyThread(THREAD *pTI)
{
std::auto_ptr<THREAD> pATI(pTI);
/*...*/
PostMessage(pATTI->hWnd, UWM_FINISHED_TEST, 0, 0);
return 0;
};

CMyDlg::OnFinishedThread(/*...*/, WPARAM wParam, LPARAM lParam)
{
CloseHandle(???); -
}

Re: getting the handle for a thread by Abdo

Abdo
Mon Sep 25 12:33:27 CDT 2006


"PaulH" <paul.heil@gmail.com> wrote in message
news:1159201155.418186.23550@m7g2000cwm.googlegroups.com...
> I have a bunch of threads created with CreateThread(). When each thread
> is exiting, it posts a message saying that it's about to quit. I'd like
> to do the CloseHandle() on the thread handle in that message handler.
> But, how can I tell which handle is the one for that particular thread
> which just finished? Is there a call like GetThreadHandle() I can use
> from within the thread to pass back the same handle I got from
> CreateThread() to the message-handler?
Yes, GetCurrentThread()

--
Abdo Haji-Ali
Programmer
In|Framez



Re: getting the handle for a thread by Igor

Igor
Mon Sep 25 12:43:39 CDT 2006

PaulH <paul.heil@gmail.com> wrote:
> I have a bunch of threads created with CreateThread(). When each
> thread is exiting, it posts a message saying that it's about to quit.
> I'd like to do the CloseHandle() on the thread handle in that message
> handler. But, how can I tell which handle is the one for that
> particular thread which just finished?

Handles are a bad way to identify threads - there may be multiple
handles to the same thread. A thread can obtain a handle to itself, but
it would be a different handle from that returned by CreateThread.

Luckily, each thread has a unique identifier - a thread ID. A thread can
obtain its own with GetCurrentThreadId. CreateThread returns the ID of
the newly created thread via lpThreadId parameter. You can also obtain
an ID from a handle with GetThreadId.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: getting the handle for a thread by PaulH

PaulH
Mon Sep 25 13:06:47 CDT 2006


Igor Tandetnik wrote:
> PaulH <paul.heil@gmail.com> wrote:
> > I have a bunch of threads created with CreateThread(). When each
> > thread is exiting, it posts a message saying that it's about to quit.
> > I'd like to do the CloseHandle() on the thread handle in that message
> > handler. But, how can I tell which handle is the one for that
> > particular thread which just finished?
>
> Handles are a bad way to identify threads - there may be multiple
> handles to the same thread. A thread can obtain a handle to itself, but
> it would be a different handle from that returned by CreateThread.
>
> Luckily, each thread has a unique identifier - a thread ID. A thread can
> obtain its own with GetCurrentThreadId. CreateThread returns the ID of
> the newly created thread via lpThreadId parameter. You can also obtain
> an ID from a handle with GetThreadId.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925

Just what I needed to know. Thanks to both of you.

-PaulH


Re: getting the handle for a thread by Alexander

Alexander
Mon Sep 25 21:42:38 CDT 2006

The owner object (creator) of the threads should keep track of them, to make
sure all threads finish before the owner object is destroyed.
Otherwise you'll be fighting with nasty race conditions. This means the
owner should keep the threads' handles and IDs.

"PaulH" <paul.heil@gmail.com> wrote in message
news:1159201155.418186.23550@m7g2000cwm.googlegroups.com...
>I have a bunch of threads created with CreateThread(). When each thread
> is exiting, it posts a message saying that it's about to quit. I'd like
> to do the CloseHandle() on the thread handle in that message handler.
> But, how can I tell which handle is the one for that particular thread
> which just finished? Is there a call like GetThreadHandle() I can use
> from within the thread to pass back the same handle I got from
> CreateThread() to the message-handler?
>
> Thanks,
> PaulH
>
> std::vector<HANDLE> m_vThreads;
>
> CMyDlg::StartAThread()
> {
> THREAD_INFO *pTI = new THREAD_INFO
> /*...*/
> m_vThreads.push_back(AtlCreateThread(MyThread, pTI);
> }
>
> static DWORD __stdcall MyThread(THREAD *pTI)
> {
> std::auto_ptr<THREAD> pATI(pTI);
> /*...*/
> PostMessage(pATTI->hWnd, UWM_FINISHED_TEST, 0, 0);
> return 0;
> };
>
> CMyDlg::OnFinishedThread(/*...*/, WPARAM wParam, LPARAM lParam)
> {
> CloseHandle(???); -
> }
>