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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:16:08+00:00 2026-06-03T04:16:08+00:00

I am trying to give each thread some thread-specific data; can this be done

  • 0

I am trying to give each thread some thread-specific data; can this be done using the thread parameter?

So when creating the thread, I pass a variable, and in the thread function, I change its value and use it as a specific data for each thread?

int main(void){
    ...
    int Tparam = 0;
    ...
    rc = pthread_create(&threads[c1], NULL, Thread_Pool, (void *)Tparam);
    ...
}

then in the Thread_Pool function I use it like this

void *Thread_Pool(void *param){
    int id;
    id = (int) param;
    id = value; // can this value be a specific value for this thread?
}
  • 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-03T04:16:10+00:00Added an answer on June 3, 2026 at 4:16 am

    It might help if you showed how Tparam was declared.

    However, if you want each thread to have its own space in which to store some data, then you can arrange to pass that space into the thread as the argument to the function. For example:

    enum { NTHREADS = 10 };
    
    struct TLS_Data
    {
        int   id;
        char  buffer[2048];
        size_t index;
    } data[NTHREADS];
    
    for (int c1 = 0; c < NTHREADS; c1++)
    {
        data[c1].index = c1;
        data[c1].id = 0;
        data[c1].buffer[0] = '\0';
        int rc = pthread_create(&threads[c1], NULL, Thread_Pool, &data[c1]);
        ...handle errors, etc...
    }
    

    Note the absence of a cast on the last argument to pthread_create(); it isn’t necessary to convert a pointer to void * when there’s a prototype in scope.

    In your Thread_Pool, you seem to want to treat the parameter as an integer; that can be done too. It is most cleanly done passing a pointer to the integer; you can pass the integer value directly if you really insist:

    uintptr_t value = c1 + 10;
    
    rc = pthread_create(&threads[c1], NULL, Thread_Pool, (void *)value);
    

    Because the type of value is uintptr_t, you know it is capable of holding a void pointer, and so it gives you the maximum chance of things working. But you’re fighting the type system, which makes it harder to write clean code.

    The one thing to be aware of is that you need to ensure that the data passed to the thread function (Thread_Pool() in your example) is not shared between threads if they are supposed to see different values. There is no guarantee of the order of thread execution, so if you made a mistake such as:

    uintptr_t value = c1 + 10;
    
    rc = pthread_create(&threads[c1], NULL, Thread_Pool, &value);
    

    (and that code was in a loop), then there’d be no guarantee about what each thread function would see. Be cautious!

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

Sidebar

Related Questions

Using PHP, I'm trying to give each specific text its own variable. I believe
In order to access this data via ajax, how can I give each json
I am trying to give the effect of general headings in this table and
I've got the following situation: <h2>This text is <span>pretty awesome</span></h2> I'm trying to give
I am trying to give each MOVIE there own url name, for example, www.helloworld.com/BATMAN.
Hello I'm trying to give each of these button names a uniquely numbered name.
I am currently trying to achieve thread synchronization in C++ .net using only atomic
What I am trying to do is give each item in my listView to
I need some help about optimisation. I am trying to improve this open-source game
So I am creating a web app, that will give each registered user a

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.