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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:36:27+00:00 2026-05-14T07:36:27+00:00

Iam new to c programming and needs some help. long *taskids[NUM_THREADS]; for(t=0; t<NUM_THREADS; t++)

  • 0

Iam new to c programming and needs some help.

long *taskids[NUM_THREADS];

for(t=0; t<NUM_THREADS; t++)
{
  taskids[t] = (long *) malloc(sizeof(long));
 *taskids[t] = t;
  printf("Creating thread %ld\n", t);
  rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]);
  ...
}

This code fragment demonstrates how to pass a simple integer to each thread. The calling thread uses a unique data structure for each thread, insuring that each thread’s argument remains intact throughout the program. Iam not able to understand how this is happening can somebody explain it??

  • 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-14T07:36:28+00:00Added an answer on May 14, 2026 at 7:36 am

    What this code is doing is storing an array of pointers to allocated data. Each thread has space allocated for a long and is then passed a pointer to that value. This gives the thread a place to work that the main thread still has access to.

    This could be done with a little less allocation:

    long taskids[NUM_THREADS];
    
    for(t=0; t<NUM_THREADS; t++)
    {
      taskids[t] = t;
      printf("Creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &taskids[t]);
      ...
    }
    

    Though this does decrease the flexibility for freeing the taskids. It also requires that this function doesn’t return until all the threads have finished.

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

Sidebar

Related Questions

I am new to all the anonymous features and need some help. I have
I am new to programming, and am wondering if there is a correct way
I am new to functional programming, and now learn Haskell. As an exercise I
I am fairly new to programming and while doing a lot of reading this
I am new to C# and .NET programming. I want to design an application
I have done Java and JSP programming in the past, but I am new
I'm fairly new to C# programming. I am making a program for fun that
New to Linux programming in general. I am trying to communicate with a kernel
I am new to C# and am doing some work in an existing application.
I am new to developing for Office Forms Server / MOSS 2007. I have

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.