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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:15:59+00:00 2026-05-13T21:15:59+00:00

I’m having some problems with my program in VS .NET 2003. I initially wrote

  • 0

I’m having some problems with my program in VS .NET 2003.

I initially wrote a module that uses the pthread library to create a number of threads to process something. This runs correctly in VS .NET 2003. Then this module was used by someone else and integrated into another larger program. I’m not sure about the details, but the program creates a GUI that allows the user to select an option to run my module.

When the thread is created, a value is passed in as the thread id. The problem with my module in the GUI is that the value of the thread id is 0 for all the threads, while the thread id is correct in the module without GUI.

Here’s how the thread is created in the module:

int64_t *tid[1000];
int64_t i = 0, rc;

for (i = 0 ; i < NUM_THREADS ; i++)
{
   tid[i] = (int64_t *) malloc(sizeof(int64_t));
   *tid[i] = i;
   rc = pthread_create(&pthread, &attr, function, (void *)tid[i]);
   Sleep(1);
   if(rc)
   {
      free(tid[i]);
      exit(1);
   }
   free(tid[i]);
}

I checked the project properties of both, and the only differences between the 2 projects are listed below:

GUI - use managed extensions | my module (w/o GUI) - does not use managed extensions
In C/C++ preprocessor:
   GUI - WIN32;_DEBUG;_CONSOLE;WINDOWS | my module (w/o GUI) - none
In C/C++ Additional Options:
   GUI - /CLR | my module (w/o GUI) - no /CLR (error with /CLR: fatal error LNK1000: Internal error during BuildImage)

The code is the same, so I don’t get why the output is wrong for the GUI, unless the use of managed extensions/clr somehow makes a difference? (I’m not really sure what those are either.)

Edited to add the part of code that outputs the thread id:

void *function(void *input)
{
   int64_t threadid = *(int64_t *)input;
   printf("threadid = %ld\n", threadid);
   ...
}

Please advise.

Thank you.

Regards,
Rayne

  • 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-13T21:15:59+00:00Added an answer on May 13, 2026 at 9:15 pm

    It appears you have a race condition when passing the tid to the function – try removing the Sleep(1) and having the thread free tid instead. From your comment above, this seems to solve the issue.

    To answer your other question, when pthread_create returns to your main thread, the new thread has been created (memory allocated for thread in OS, etc) but may not have actually run yet. Putting Sleep(1) makes it more likely that the new thread will run (forces main thread to give up timeslice) but there’s no guarantee thread will get tid before main thread runs again and calls free(tid[i]);

    Hence the idea of having the worker thread free tid – it will free after it has picked it up.

    If you change your tid array to an array of int64’s and pass &tid[i] then you won’t need malloc/free and the race condition is also solved – though this only works if you’re guaranteed to have only one caller to your library at once.

    I hope this helps – getting threading right can be tricky but well worth the effort!

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Entity Framework designer is terrible - I've had the… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If by "hijack" you meant sniff the packets then what… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If you want two actions to be atomic, embed them… May 14, 2026 at 5:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.