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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:32:00+00:00 2026-05-20T09:32:00+00:00

I am creating 5 thread here using ThrdFunc. Here each thread update the listBox.

  • 0

I am creating 5 thread here using ThrdFunc. Here each thread update the listBox.

I was expecting message in this way. Initially come in this way but after some time

Thread1:Adding msg
Thread2:Adding msg
Thread3:Adding msg

But after some time I get message like

Thread0:Adding msg
Thread18967654:Adding msg
Thread18967654:Adding msg
Thread18967654:Adding msg

This is the code:

for (int i = 0;i<6;i++)
    {   
        nThreadNo = i+1;
        hWndProducer[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ProducerThrdFunc,(void*)&nThreadNo,0,&dwProducerThreadID[i]);     
        if (hWndProducer[i] == NULL) 
        {
           //ErrorHandler(TEXT("CreateThread"));
           ExitProcess(3);
        }
    }   

DWORD WINAPI    ThrdFunc ( LPVOID n )
{
    int *nThreadNo = (int*)n;       
    char chThreadNo[3];
    memset(chThreadNo,0,3);

    while(1)
    {
        itoa(*nThreadNo,chThreadNo,10);
        char* pMsg1 = new char[100];
        char* pMsg2 = new char[100];
        memset(pMsg1,0,100);
        memset(pMsg2,0,100);

        strcpy(pMsg1," Thread No:");        
        strcat(pMsg1,chThreadNo);
        strcat(pMsg1," Adding Msg:");

        PostMessage(stThreadInfoProd.hWndHandle,UWM_ONUPDATEPRODUCERLIST,(WPARAM)pMsg1,0);


    }   
    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-05-20T09:32:00+00:00Added an answer on May 20, 2026 at 9:32 am

    Most likely nThreadNo is allocated on the stack. You’re giving each thread a pointer to one of it’s elements.

    Once the function creating the threads returns, the array is no longer valid, but the thread functions are still pointing to it. The memory the threads are holding pointers to will most likely be overwritten, causing what was originally the thread ID to be overwritten with garbage.

    Anything you pass another thread should generally be allocated on heap, either via malloc type functions or new, preferably new since this is C++.

    For example, instead of int nThreadNo[6], use int* nThreadNo = new int[6]. However, keep in mind that you will have to delete[] the memory nThreadNo points to when you’re done with it.

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

Sidebar

Related Questions

I am trying to create this QT gui using a thread but no luck.
This is my first Java application I am creating (using Eclipse IDE) and the
I am creating a thread using BeginThread. In the procedure I am using to
I am creating a new boost::thread using boost::bind , and storing it in a
I am not using any thread pool. Just creating ThreadArray.The for loop creats the
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
I am working on creating a thread safe control for my windows forms application.
I found similar questions asked here but there weren't answers to my satisfaction. So
I'm creating piecharts using JFreeChart , use chart.createBufferedImage(width,height) and give the buffered image to
I have created this little program to calculate pi using probability and ratios. In

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.