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 8720045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:56:30+00:00 2026-06-13T06:56:30+00:00

I need to create 10 threads and print out the threadId with which thread

  • 0

I need to create 10 threads and print out the threadId with which thread printed.

#include <windows.h>
#include <stdio.h>

DWORD WINAPI xprint(LPVOID Param)
{
DWORD tID = GetCurrentThreadId();
int count = *(int*)Param + 1;

fprintf(stderr, "Thread Number:%d ThreadID:%d\n", count, tID);

return 0;
}

int main ()
{
   DWORD ThreadID[10];
   HANDLE ThreadHandle[10];
   int i;

   for(i=0; i<10; i++){

    ThreadHandle[i] = CreateThread(
      NULL,
          0,
          xprint,
          &i,
          0,
          &ThreadID[i]);

    if (ThreadHandle[i] == NULL) {
        fprintf(stderr,"Can't create thread!\n");
        return -1;
        }
    }

    for (i=0; i<10; i++){
            WaitForSingleObject(ThreadHandle[i],INFINITE);
        CloseHandle(ThreadHandle[i]);
    }
}

output:

Thread Number:1 ThreadID:4952
Thread Number:1 ThreadID:5284
Thread Number:1 ThreadID:5084
Thread Number:1 ThreadID:5656
Thread Number:1 ThreadID:5292
Thread Number:1 ThreadID:3072
Thread Number:1 ThreadID:5904
Thread Number:1 ThreadID:3620
Thread Number:1 ThreadID:6056
Thread Number:1 ThreadID:5676

I don’t know where i did wrong.
my desired output should look something like this.

output:

Thread Number:10 ThreadID:4952
Thread Number:8 ThreadID:5284
Thread Number:3 ThreadID:5084
Thread Number:9 ThreadID:5656
Thread Number:1 ThreadID:5292
Thread Number:2 ThreadID:3072
Thread Number:5 ThreadID:5904
Thread Number:6 ThreadID:3620
Thread Number:4 ThreadID:6056
Thread Number:7 ThreadID:5676

The Thread Number should be *out of order.

* After posting this question i ran the program a few more times
output:

Thread Number:2 ThreadID:4500
Thread Number:7 ThreadID:5740
Thread Number:7 ThreadID:3772
Thread Number:1 ThreadID:4732
Thread Number:2 ThreadID:4252
Thread Number:2 ThreadID:4264
Thread Number:2 ThreadID:5544
Thread Number:2 ThreadID:4116
Thread Number:2 ThreadID:5240
Thread Number:2 ThreadID:4804

it seems like the thread number is random. but in any case, no same number should appear twice.

  • 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-13T06:56:31+00:00Added an answer on June 13, 2026 at 6:56 am

    I’m not on a Windows machine, so I can’t test this, but in a program I wrote I made an array of all the value, and did a pointer to the array element (int numbers[100]; &number[i]). The reason this worked was cause i was changing while the second thread was running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a server which creates a new thread for each client
I need to create async thread that runs once with a delay of 2
I need to obtain the Thread Id from a window create in runtime with
I have to create a progress bar in a webapp. I need a thread
I create a thread in my main class. The thread has a timer which
I'm trying to create a map in which the entries time out and get
Client has gui and extra thread (to process socket input and print it out
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get

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.