Who owns CWinThread after it was created by AfxBeginThread?

I start the stream using AfxBeginThread. This returns a pointer to a new object CWinThread.

MSDN states that this pointer is NULL, and it will free everything if thread creation fails. However, as soon as the thread exits after regular startup, the object CWinThreadstill exists. I'm not sure if I should delete the object CWinThread, or if this is done by MFC itself (although this is not the case).

FYI is unlikely that the thread will exit, as it should work until the application terminates. However, since I use it as part of a thread pool, I do not want the CWinThreadaorund to hang forever.

+5
source share
3 answers

, . , delete , AfxBeginThread, , :

CWinThread *thread = AfxBeginThread(...);
/* ... */
// now wait for it to terminate
WaitForSingleObject(thread->m_hThread, INFINITE); 
delete thread;

CWinThread, / , . .

+2

CWinThread m_bAutoDelete. . . .

, , , , CWinThread , .

m_bAutoDelete FALSE, . , , , Using Worker Threads.

   thread = AfxBeginThread(proc, this, 
                       THREAD_PRIORITY_NORMAL, // default: use it
                       0,     // default stack size 
                       CREATE_SUSPENDED); // let us set auto delete
   if(thread) { // protect against that rare NULL return
       thread->m_bAutoDelete = FALSE;
       thread->ResumeThread();
   }
+7

CWinThread . , MFC, , :

CWinThread *thread = AfxBeginThread(...); 
thread->m_bAutoDelete = FALSE;

, .

+4

All Articles