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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:23:47+00:00 2026-06-13T11:23:47+00:00

Here is a block of code that creates a number of threads provided by

  • 0

Here is a block of code that creates a number of threads provided by the user, each thread then generates a random number and calculates its squareroot. I cannot figure out why the threads are getting the same ID, line 64 is the culprit as it is where the threads are being created. I suspect that there is something happening in the loop that is causing the threads to all be generated at the same time.

////////////////////////////////////////////////
//
//
// Zach
//
// 
//
//
////////////////////////////////////////////////



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>





void *squareroot(void *num1)
{
  int *id = (int *)num1;
  int incoming = rand()/100;
  float *outgoing = (float *)malloc(sizeof(float));
  printf("%d \n", *id);
  printf("%d\n", incoming);
  *outgoing = 5.000;
  //Calculate the square root of the number passed to the function
  *outgoing = sqrt(incoming);
  return outgoing;

}


int main(int argc, char* argv[])//testing funcion
{
  srand(time(NULL));
  int i, j;
  int *temp = (int *)malloc(sizeof(int));
  if (argc != 2) 
  {

    printf ("ERROR: Enter a number\n");
    return 1;

  }

  int loop = atoi(argv[1]); //grabbing the integer supplied by user
  pthread_t thread_id[loop];
  void *exit_status;
  float *thread_result;

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

    pthread_create(&thread_id[i], NULL, squareroot, &i);
  }


  for(j = 0; j < loop; j++)
  {
    pthread_join(thread_id[j], &exit_status);
    thread_result = (float *)exit_status;
    printf("%f\n", *thread_result);

  }


}
  • 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-13T11:23:50+00:00Added an answer on June 13, 2026 at 11:23 am

    I think what is happening is that your loop finishes creating all the threads (or at least some of them) before any of the threads actually run and extract their unique id.

    Because you’re passing a pointer to i, when each thread finally gets around to checking its parameter, i is already finished… Or at least partway through. The danger is that multiple threads might see the same value for i. It’s even worse that you never copy the value out of that pointer – you always dereference it. That means it might change in the middle of your thread’s execution.

    What you should do instead is pretend it is a pointer:

    pthread_create(&thread_id[i], NULL, squareroot, (void*)i);
    

    And in your thread function:

    int id = (int)num1;
    

    This works because the pointer is passed by value. Whatever value you provide is the value that goes into the thread function. Previously it didn’t work because you passed a pointer to a value that could change in another thread.

    PS: Don’t forget to free the result from each thread in your loop at the end. At the moment you’re not cleaning up memory that you allocated.

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

Sidebar

Related Questions

I've got a block of code that's doing what I want - it generates
here is my code: $(a).live('click', function(){ history.pushState({}, '', this.href); popstate(this.href); $(#loadpage).css(display, block); return false;
I have a block of text that im taking from a Gedcom ( Here
I have a block of code that runs within a TransactionScope and within this
Here is the code that I use to create a table, a sequence and
Here is my code snippet where I'am taking confidence with initialization blocks class Father{
Here is my block of CSS: .actual-form table { padding: 5px 0 15px 15px;
Ok here is what I want to do: I pass a block to a
Here is what I am trying to do. I have a block of text
See here http://www.hanselman.com/blog/InSearchOfThePerfectMonospacedProgrammersFontInconsolata.aspx - for want of a better description - the statement block

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.