所以我想做的是编写一个程序,创建一系列子线程,这些子线程使用 pthread_create 方法获取参数,并使用传入的参数进行更多操作等等。我试图传入的参数是一个名为 reduce_args_ 的向量参数。这是结构 ReduceVector 的标头信息。
typedef vector<string> StringVector;
// a data structure to maintain info for the reduce task
struct ReduceArg
{
ReduceArg (void); // constructor
~ReduceArg (void); // destructor
pthread_t tid; // thread id of the reduce thread
StringVector files_to_reduce; // set of files for reduce task
};
// more typedefs
typedef vector<ReduceArg *> ReduceVector;
现在问题来了,当我在这里调用 push_back 时:
for(int i = 0; i < num_reduce_threads_ ; i++){
reduce_args_.push_back(phold);
int count = 0;
for(ShuffleSet::iterator it = shuffle_set_.begin(); it!=shuffle_set_.end(); ++it){
string line = *it;
string space = " ";
string file = line.substr(0, line.find(space)) + ".txt";
if (count < num_reduce_threads_){
cout << reduce_args_[i+1];
(reduce_args_[i+1] -> files_to_reduce)[count] = file;
//(reduce_args_[i+1] -> files_to_reduce).push_back(file);
}
count++;
//cout << ((reduce_args_.back())->files_to_reduce).back()<< endl;
}
}
这两种 push_back 方法都会导致段错误。洗牌集只是一个集合,正在输出字符串。并且如 .h 文件中所述,files_to_reduce 是一个字符串向量。所以我想要做的是访问 files_to_reduce 并 push_back 一个字符串到它上面,但每次我遇到一个段错误。reduce_args_ obj 声明如下:
ReduceArg* plhold;
reduce_args_.push_back(plhold);
((reduce_args_.back()) -> files_to_reduce).push_back("hello");
for (int i = 0; i < this->num_reduce_threads_; ++i) {
// create a placeholder reduce argument and store it in our vector
(reduce_args_.push_back(plhold));
}
谢谢您的帮助!!