RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 890495
Accepted
Denis Danilov
Denis Danilov
Asked:2020-10-08 20:24:39 +0000 UTC2020-10-08 20:24:39 +0000 UTC 2020-10-08 20:24:39 +0000 UTC

将向量传递给 C++ 函数时出错

  • 772

在用 c++ 编写多线程应用程序时,有必要将大量对向量的引用传递给以多线程模式 (multy) 运行的函数,这些函数对每个线程都有自己的值。编译导致错误:

  1. 严重性代码说明项目文件行抑制状态错误C2672 'std::invoke': 找不到匹配的重载函数
  2. 严重性代码描述项目文件行抑制状态错误 C2893 无法专门化函数模板“未知类型 std::invoke(_Callable &&,_Types &&...) noexcept()”

控制台应用程序代码如下:

#include "pch.h"
#include "libraries.h"
#include "globalVariables.h"
//#include "multyThreadingFunc.h"


thread_local double centerOfMass = 0.0;

thread_local double templateZ;


vector<float> distanceAxis(n_bin + 1);

//here the total number of molecules in each bin is saved during the loop for the analyte molecules
vector<double> vectordensity(n_bin);

mutex mtx;

void multy(FILE &fid, int &amountOfAtoms, float &time, vector<float> &coordinates, vector<float> &changedCoord, double &centerOfMass, double &templateZ, vector<double> &vectordensity) {

    FILE *file = &fid;
    ...//some calculating
}

int main()
{
    thread_local vector<float> coordinates(3); //x,y,z coordinates
    thread_local vector<double> changedCoord;

    FILE *fid = fopen("Benzene_Simu1_0_35ns.gro","rt"); 
    if (fid == NULL) throw exception("Error was happend with the file");

    float time;

    for (int i = 1; i <= n_bin; i++) {
        distanceAxis[i] = (i - 0.5)*BinWidth;
    }

    int startTime = clock();
    while (true) {
        std::thread thr(multy, ref(fid), &amountOfAtoms, &time, ref(coordinates), ref(changedCoord), &centerOfMass, &templateZ, ref(vectordensity));

        if (feof(fid)) break;

        vectordensity[bin]++;   //sorts the molecules according to their bins
                                //after finishing the loop, density contains the distribution of the molecules over all bins for all time steps

        index++;
    }
    --index;

    fclose(fid);
    cout << endl << (endTime - startTime) / 1000 << endl;
    return 0;
}
c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    AnT stands with Russia
    2020-10-08T20:53:50Z2020-10-08T20:53:50Z

    向流传递参数时写了一些废话。例如,在流函数中,参数

    int &amountOfAtoms
    

    是对 的引用int,当你创建一个流时,你会通过那里

    &amountOfAtoms
    

    那些。指向int. 其他选项也存在此错误。难怪编译器不能调用线程函数。很明显,您正确地传递了对向量的引用,但由于某种原因,您没有传递对标量值的引用。这种划分从何而来?

    使用参数fid,发生了一些难以理解的事情。为什么当线程函数需要时它被传递ref(fid)(即实际上) ?它是什么,为什么需要这个?你为什么不马上派一个正常人来?FILE *&FILE &FILE &FILE &FILE *

    此外,我没有在您的代码中看到任何尝试“对每个线程都有自己的值的向量进行一定数量的引用”。为了让每个线程都有一个向量的“自己的值”,有必要为每个线程创建一个单独的向量实例。但我在任何地方都看不到它。所有线程都接收对相同向量的引用。thread_local特别是,如果所有线程都从主线程接收到对相同数据的引用并使用它们,则不清楚为什么要在代码中设置这些说明符。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5