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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:31:30+00:00 2026-06-05T06:31:30+00:00

I have a process that creates a number of threads based on an argument

  • 0

I have a process that creates a number of threads based on an argument passed to the process.

producer_threads[num_threads];
for (id = 0; id < num_threads; id++)
{
    printf("%d\n", id);
    pthread_create(&producer_threads[id], NULL, &produce, (void *) &id);
}

Each thread goes into a produce function and stores the id as a local variable

void* produce (void* args)
{
    int my_id = * (int*) args;
    printf("Thread %d started to produce\n", my_id);
}

However the output I receive is as shown

0
1
Thread <n> started to produce
Thread <n> started to produce

and n are randomly either 0, 1, or 2. I am not sure what is causing the problem unless it is because the global variable is being updated before it is assigned locally. Or because the “local variable” is shared between threads.

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

    The problem is that you’re passing pointers to the same variable to each thread. This creates a race condition, whereby the value of the variable as seen by each thread depends on the exact timing.

    If you were to pass the thread argument by value rather than by pointer, this would fix the problem.

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

Sidebar

Related Questions

I have some code that creates a Process instance and later starts it. There's
In my ASP.NET MVC application I have a number of threads that wait for
I have a process that must create and close threads on demand. Each thread
I have event manager process that dispatches events to subscribers (e.g. http_session_created, http_sesssion_destroyed). If
I am to process single PDFs that have each been created by 'merging' multiple
I have Process objects that are monitored from two different views. A Windows.Forms.ListView (actually
I have a process that does a composite of two images, and it works
We have a process that is computationally intensive. When it runs it typically it
I have a process that interfaces with a library that launches another process. Occasionally
I have a process that spawns a helper process. Sometimes I need to debug

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.