正确通过参考通过ARG并在功能中更新其值的方法

发布于 2025-02-04 16:35:35 字数 1754 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世蒼涼 2025-02-11 16:35:35

通过参考通过arg的正确方法是通过参考实际通过args 。

auto f = [&](vector<vector<double>> matrix, vector<double> x,
    vector<double> b, vector<double> new_x, int i, int n){ ... }

所有参数都是按价值传递的。您还可以通过参考来绑定其他所有内容,并且永远不会使用它。

我认为您正在误以为[&amp;]做什么。

您的lambda不需要绑定任何东西,只需通过参数作为参考即可。

auto f = [](const vector<vector<double>> & matrix, const vector<double> & x,
    cost vector<double> & b, vector<double> & new_x, int i, int n){ ... }

The correct way to pass args by reference is drumm roll to actually pass args by reference.

auto f = [&](vector<vector<double>> matrix, vector<double> x,
    vector<double> b, vector<double> new_x, int i, int n){ ... }

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.

auto f = [](const vector<vector<double>> & matrix, const vector<double> & x,
    cost vector<double> & b, vector<double> & new_x, int i, int n){ ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文