我想在SimGrid中创建一个包含数据的进程,所以我这样做:
int worker(int argc, char *argv[])
{
vector<string> vector1;
vector1.push_back("word");
MSG_process_create("x", executor, &vector1, MSG_host_self());
return 0;
}
但在executor
我有分段错误:
int executor(int argc, char* argv[]){
MSG_process_sleep(10);
vector<string> *data = (vector<string>*) MSG_process_get_data(MSG_process_self());
XBT_INFO("%s", data->front().c_str());
return 0;
}
我知道发生这种情况是因为当vector1
函数结束且worker
消失时vector1
超出范围,所以&#34;没有vector1数据&#34;在遗嘱执行人。
如何正确地做到这一点?
答案 0 :(得分:1)
我想你只想在这里使用指针:)
int worker(int argc, char *argv[])
{
vector<string>* vector1 = new vector<string>();
vector1->push_back("word");
MSG_process_create("x", executor, vector1, MSG_host_self());
return 0;
}
int executor(int argc, char* argv[]){
MSG_process_sleep(10);
vector<string> *data = (vector<string>*) MSG_process_get_data(MSG_process_self());
XBT_INFO("%s", data->front().c_str());
delete data;
return 0;
}