Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8417461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:03:25+00:00 2026-06-10T02:03:25+00:00

I have a loop generating threads via AfxBeginThread , which stores the CWinThread pointers

  • 0

I have a loop generating threads via AfxBeginThread, which stores the CWinThread pointers in an array. In each iteration, I check the thread is not null and store the thread’s handle in another array.

const unsigned int maxThreads = 2;
CWinThread* threads[maxThreads];
HANDLE* handles[maxThreads];
for(unsigned int threadId=0; threadId < maxThreads; ++threadId)
{
    threads[threadId] = AfxBeginThread(endToEndProc, &threadId,
                                            0,0,CREATE_SUSPENDED);
    if(threads[threadId] == NULL) 
    {
        // die carefully
    }
    threads[threadId]->m_bAutoDelete = FALSE;
    handles[threadId] = &threads[threadId]->m_hThread;
    ::ResumeThread(handles[threadId]);
}

DWORD result = ::WaitForMultipleObjects(maxThreads, handles[0], 
                                            TRUE, 20000*maxThreads);

But WaitForMultipleObjects always returns with WAIT_FAILED, and GetLastError yields 6 for invalid handle. Either the test for the AfxBeginThread return is insufficient to guarantee the thread was created successfully and the handle will be valid, or the handle is becoming invalid before the WaitForMultipleObjects call, which I thought would be prevented by setting m_bAutoDelete to FALSE.

Is there a better way to wait on multiple threads when they are created by AfxBeginThread?

Note that it is fine when maxThreads=1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:03:26+00:00Added an answer on June 10, 2026 at 2:03 am

    handles[0] points to something that has ONE valid handle and some data possibly following it. maxThreads instead suggests that array should have two handles there one after another. Hence the error.

    This is what you want instead:

    HANDLE handles[maxThreads];
    //...
    handles[threadId] = threads[threadId]->m_hThread;
    //...
    WaitForMultipleObjects(maxThreads, handles, ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have loop and each iteration of Hash looks like this: [1, {:clid=>1, :nvz=>4,
I have a loop which is generating some data and in certain cases I
I have a PHP loop that's auto-generating an array to INSERT into a table.
I have a for loop generating integers. For instance: for (int i=300; i>200; i--)
I have a loop that enters each non-empty text field into a database: foreach
I have a loop $(.box).find('.video, .print, .web').closest('.box').each(function (index) { $(this).delay(100 * index).fadeIn(300); }); This
I have a loop in razor which generates a template a number of times.
I have the following array with a structure within each array (see this jsfiddle
So I have this simple PHP loop that is generating html table data with
I have am generating the array at the bottom of this message. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.