如何在SimGrid中创建一个带矢量的过程?

时间:2016-10-05 17:13:54

标签: c++ vector simgrid

我想在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;在遗嘱执行人。

如何正确地做到这一点?

1 个答案:

答案 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;
}