I have a newbie question about Boost::Thread
and Mutex
.
I would like to start many parallel instances of the following Worker
, and all of them write to the same std::vector
:
struct Worker {
std::vector<double>* vec;
Worker(std::vector<double>* v) : vec(v) {}
void operator() {
// do some long computation and then add results to *vec, e.g.
for(std::size_t i = 0; i < vec->size(); ++i) {
(*vec)[i] += some_value;
}
}
};
I understand that the Worker has to lock vec
before it write to it and unlock it when it's done (because all Workers write to the same vector). But how do I express that?