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

  • Home
  • SEARCH
  • 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 8854593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:55:12+00:00 2026-06-14T13:55:12+00:00

The code below does compile and run except that the output is not what

  • 0

The code below does compile and run except that the output is not what I expect it to be. I wanted the program to create n-number of threads (depending on the number of multicores available) and run a simple routine found at the end of the program for outputting

“testing:”

followed by a number 1-10. What I get instead is an output that does write a few numbers but it doesn’t go past two at the most and the function threadmain doesn’t seem to completely run on one thread but it somewhat outputs testing: 012 in others. I know multithreading will mangle output but I should see the numerals 3,4,5,6,7,8,9 somewhere on the screen but it doesn’t show up.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <cmath>
#include <iostream>

HANDLE *m_threads = NULL;
static DWORD_PTR WINAPI threadMain(void* p);

DWORD_PTR GetNumCPUs()
{
  SYSTEM_INFO m_si = {0, };
  GetSystemInfo(&m_si);
  return (DWORD_PTR)m_si.dwNumberOfProcessors;
}

CRITICAL_SECTION g_crit_sec;
static int g_start = 0;

int main(int argc, char **args)
{
  DWORD_PTR c = GetNumCPUs();

  m_threads = new HANDLE[c];

  InitializeCriticalSectionAndSpinCount(&g_crit_sec, 0x80000400);

  for(DWORD_PTR i = 0; i < c; i++)
  {
    DWORD_PTR m_id = 0;
    DWORD_PTR m_mask = 1 << i;

    m_threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadMain, (LPVOID)i, NULL, &m_id);
    SetThreadAffinityMask(m_threads[i], m_mask);

    //wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)m_threads[i], m_mask);
  }

  return 0;
}
static DWORD_PTR WINAPI threadMain(void* p)
{
    double result = 0.0;

    for (int i = 0; i < 10; i++)
    {
        std::cout<<"testing: "<<i<<std::endl;
        //result = result + sin(i) * tan(i);
    }

  return 0;
}
  • 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-14T13:55:14+00:00Added an answer on June 14, 2026 at 1:55 pm

    Your application is terminating before your threads are done running. Put a Sleep() or more appropriately a WaitForMultipleObjects() call in your main function after starting the threads, like this:

    int main(int argc, char **args)
    {
      DWORD_PTR c = GetNumCPUs();
    
      m_threads = new HANDLE[c];
    
      InitializeCriticalSectionAndSpinCount(&g_crit_sec, 0x80000400);
    
      for(DWORD_PTR i = 0; i < c; i++)
      {
        DWORD_PTR m_id = 0;
        DWORD_PTR m_mask = 1 << i;
    
        m_threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadMain, (LPVOID)i, NULL, (LPDWORD)&m_id);
        SetThreadAffinityMask(m_threads[i], m_mask);
    
        //wprintf(L"Creating Thread %d (0x%08x) Assigning to CPU 0x%08x\r\n", i, (LONG_PTR)m_threads[i], m_mask);
      }
    
      // Waits for all started threads to complete:
      WaitForMultipleObjects(c, m_threads, TRUE, INFINITE);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the code snippet below, why does 1 not produce a run-time Exception as
Consider the code below. I don't understand why my GCC compiler does not try
The code below does not work but meant to illustrate what I want to
Why does the code below work fine when I run my web application localhost
I have the code below. I'm wondering that does the self.value and _value have
I have stumbled upon the following problem. The below code snippet does not link
I know that android version 2.1 and below do not support GLES20. My code
Below class does not compile, if I declare Functions as an object instead of
Why does the below code give Seg. Fault at last line? char* m=ReadName(); printf(\nRead
Why does the code below result in org.datanucleus.exceptions.NucleusUserException: Object Manager has been closed? The

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.