正确通过参考通过ARG并在功能中更新其值的方法
我在C ++中编写了一个程序,该程序使用绑定任务的队列和从队列中获取任务的螺纹池。线程必须在矩阵和向量之间执行操作,并将结果写入另一个向量。
问题在于,当线程退出执行的函数时,没有写入向量的新值。通过参考并更新参数的正确方法是什么?
mutex ll;
condition_variable cond;
bool stop = false;
deque<function<void()>> bind_tasks;
// initialize the vector with all zeroes
vector<double> x(n, 0.0), new_x(n, 0.0);
auto bind_submit = [&] (function<void()> f){
{
unique_lock<mutex> lock(ll);
bind_tasks.push_back(f);
}
cond.notify_all();
};
auto body = [&](int tid){
while(true){
function<void()> t = []() {return 0; };
{
unique_lock<mutex> lock(ll);
cond.wait(lock,
[&]() { return (!bind_tasks.empty() || stop); }
);
if (!bind_tasks.empty()) {
t = bind_tasks.front();
bind_tasks.pop_front();
}
if (stop)
return;
}
t();
}
};
// executed by the threads
auto f = [&](vector<vector<double>> matrix, vector<double> x,
vector<double> b, vector<double> new_x, int i, int n){
int sum = 0;
// use the values of x and writes on new_x
for (int j = i + 1; j < n; j++) {
sum = matrix[i][j] * x[j];
}
new_x[i] = (b[i] - sum) / matrix[i][i];
};
vector<thread> tids(nw);
for(int tid=0; tid<nw; tid++){
tids[tid] = thread(body, tid);
}
for(int it=0; it<k; it++){
for(int i=0; i<n;i++){
auto fx = (bind(f, matrix, x, b, new_x, i, n));
bind_submit(fx);
}
//function that prints the vector
print_vector(new_x); //it is still all zeroes
x=new_x;
}
I am writing a program in c++ that uses a queue of bind tasks and a threadpool that fetches tasks from the queue. The threads must perform operations between a matrix and a vector and write the result into another vector.
the problem is that when the function executed by the thread exits, no new values have been written into the vector. What is the right way to pass the arguments by reference and update them?
mutex ll;
condition_variable cond;
bool stop = false;
deque<function<void()>> bind_tasks;
// initialize the vector with all zeroes
vector<double> x(n, 0.0), new_x(n, 0.0);
auto bind_submit = [&] (function<void()> f){
{
unique_lock<mutex> lock(ll);
bind_tasks.push_back(f);
}
cond.notify_all();
};
auto body = [&](int tid){
while(true){
function<void()> t = []() {return 0; };
{
unique_lock<mutex> lock(ll);
cond.wait(lock,
[&]() { return (!bind_tasks.empty() || stop); }
);
if (!bind_tasks.empty()) {
t = bind_tasks.front();
bind_tasks.pop_front();
}
if (stop)
return;
}
t();
}
};
// executed by the threads
auto f = [&](vector<vector<double>> matrix, vector<double> x,
vector<double> b, vector<double> new_x, int i, int n){
int sum = 0;
// use the values of x and writes on new_x
for (int j = i + 1; j < n; j++) {
sum = matrix[i][j] * x[j];
}
new_x[i] = (b[i] - sum) / matrix[i][i];
};
vector<thread> tids(nw);
for(int tid=0; tid<nw; tid++){
tids[tid] = thread(body, tid);
}
for(int it=0; it<k; it++){
for(int i=0; i<n;i++){
auto fx = (bind(f, matrix, x, b, new_x, i, n));
bind_submit(fx);
}
//function that prints the vector
print_vector(new_x); //it is still all zeroes
x=new_x;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过参考通过arg的正确方法是通过参考实际通过args 。
所有参数都是按价值传递的。您还可以通过参考来绑定其他所有内容,并且永远不会使用它。
我认为您正在误以为
[&amp;]
做什么。您的lambda不需要绑定任何东西,只需通过参数作为参考即可。
The correct way to pass args by reference is drumm roll to actually pass args by reference.
All the arguments are passed by value. You also bind everything else by reference and never use it.
I think you are missundestanding what
[&]
does.Your lambda doesn't need to bind anything, just pass arguments as reference.