我发现很难解释这个问题,所以我会发布代码并解释发生了什么,然后询问如何让它做我想要的。首先,我在子进程中创建一个线程:
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);
}
这是struct arg_struct
:
struct arg_struct{
string name;
int num_req;
int curr_value;
int buff_size;
};
和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;
}
在利用local_name
和的两条线中local_num_req
,存在问题。我编译得很好,但是这两个变量似乎每次都存储不同的东西。有时它可以正常工作,而有时它们保存垃圾值并且程序从不执行它们(或任何后续)。我尝试使用args->name
没有局部变量的原始名称(即),但问题是一样的。我最好的猜测是我args_struct
错误地处理了变量,但我不知道为什么它只会在部分时间失败。
如何在 中获得正确的变量值mythreadfunc
?