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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:09:14+00:00 2026-06-08T13:09:14+00:00

I find it hard to explain the problem, so I’ll post the code and

  • 0

I find it hard to explain the problem, so I’ll post the code and explain what happens, and then ask how to make it do what I want. Firstly, I create a thread within a child process:

pid_t childpid = fork();

if(childpid == -1){
  cout << "Failed to fork." << endl;   
}
else if(childpid == 0){     
  //request threads
  pthread_t p1, p2, p3;          

  struct arg_struct args1, args2, args3;

  args1.name = "data Joe Smith";
  args1.num_req = n;
  args1.buff_size = b;
  pthread_create(&p1, NULL, &mythreadfunc, (void *)&args1);   
}

This is the struct arg_struct:

struct arg_struct{
    string name;
    int num_req;
    int curr_value;
    int buff_size;
};

And the mythreadfunc:

void *mythreadfunc(void *arguments){ 
    struct arg_struct *args = (struct arg_struct *)arguments;
    string local_name = args->name;     
    int local_num_req = args->num_req;
    //request new thread
    RequestChannel chan("control", RequestChannel::CLIENT_SIDE);
    cout << "done." << endl;
    string reply1 = chan.send_request("newthread");

    cout << "Reply to request 'newthread' is " << reply1 << "'" << endl;

    RequestChannel chan2(reply1, RequestChannel::CLIENT_SIDE); 

    cout<<"local_name:  "<<local_name<<endl;    //returns incorrect value***
    cout<<"local_num_req:  "<<local_num_req<<endl;  //returns incorrect value***

    //close up all channels
    string reply2 = chan2.send_request("quit");
    cout << "Reply to request 'quit' is '" << reply2 << "'" << endl;
    string reply3 = chan.send_request("quit");
    cout << "Reply to request 'quit is '"<< reply3 << "'" << endl;
}

In the two lines that utilize local_name and local_num_req, there are problems. I compile fine, but the two variables always seem to store something different each time. Sometimes it works properly, while sometimes they hold garbage values and the program never executes them (or anything following). I tried to use the original names (i.e. args->name) without the local variables, but the problem is the same. My best guess is that my args_struct handles the variables wrong, but I don’t know why it would only fail part of the time.

How can I have the correct variable values in the mythreadfunc?

  • 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-08T13:09:17+00:00Added an answer on June 8, 2026 at 1:09 pm

    Create the argument for your new thread on the heap, it’s going out of scope in the calling function and so isn’t valid for use by your thread(s):

    struct arg_struct *arg1 = new arg_struct;
    
    args1->num_req = n;
    // etc...
    
    pthread_create(&p1, NULL, &mythreadfunc, (void *) args1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this small problem but I find it hard to explain so bear
As with many inheritance problems, I find it hard to explain what I want
I find it hard to explain, so please request more info if you need
I find it hard to find clear examples that would explain how to read
I usually play with elisp code on my scratch buffer. I find it hard
I am new to PHP and find it very hard to explain. I have
This is something that I always find a bit hard to explain to others:
This problem is a little hard to explain. I'll do my best, hoping to
Although it is hard to explain, I am writting to find a solution. I
I find it really hard to explain what is going on, I'll try my

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.