Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向量处理的多线程_C++_Multithreading - Fatal编程技术网

C++ 向量处理的多线程

C++ 向量处理的多线程,c++,multithreading,C++,Multithreading,我有2个向量,其中包含一些文件名和处理这些文件的函数: vector<string> vecFilenames1; // {filename1_1, filename1_2, filename1_3, ...} vector<string> vecFilenames2; // {filename2_1, filename2_2, filename2_3, ...} 我是否需要将其更改为新版本以方便解决我的问题 编辑2 我更新了我的gcc: g++.exe (MinGW.

我有2个向量,其中包含一些文件名和处理这些文件的函数:

vector<string> vecFilenames1; // {filename1_1, filename1_2, filename1_3, ...}
vector<string> vecFilenames2; // {filename2_1, filename2_2, filename2_3, ...}
我是否需要将其更改为新版本以方便解决我的问题

编辑2

我更新了我的gcc:

g++.exe (MinGW.org GCC Build-2) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.

您应该将向量划分为多个范围,并在线程池线程中处理每个范围

C++17并行算法是实现这一点的简单方法。通过使用std算法,您不需要执行诸如划分向量和手动调用线程池之类的操作

在不支持C++17的情况下,您可以使用“英特尔TBB库”或Open MP指令来实现类似的功能

或者推出自己的实现
std::async
是运行线程池任务,
std::hardware\u concurrency
来获得内核数量的估计值

每个的并行
示例:

#include <algorithm>
#include <chrono>
#include <iostream>
#include <execution>
#include <mutex>
#include <string>
#include <thread>

using namespace std;

vector<string> vecFilenames1;
vector<string> vecFilenames2;

int main() {
    for (int i = 1; i < 1000; i++)
    {
        vecFilenames1.push_back("filename1_" + to_string(i));
        vecFilenames2.push_back("filename2_" + to_string(i));
    }

    mutex m;

    auto f = [&](const string& fn1)
    {
        // Comupute other element via pointer arthimetics
        // Works only with vector, for, say, deque, use container of struct
        const string& fn2 = vecFilenames2[&fn1 - vecFilenames1.data()];

        // simulate processing (to hide mutex unfairness and make threads
        // working concurrently)
        // replace with read processing
        using namespace chrono_literals;
        this_thread::sleep_for(30ms);

        // avoid doing any real work under the lock to benefit from paralleling
        lock_guard<mutex> guard(m);

        // ideally don't do console i/o from thread pool in real code
        cout << "Processing " << fn1 << " & " << fn2
            << " from " << this_thread::get_id() << '\n';
    };

    for_each(execution::par, vecFilenames1.begin(), vecFilenames1.end(), f);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
向量向量1;
向量向量2;
int main(){
对于(int i=1;i<1000;i++)
{
vecFilenames1.向后推_(“filename1_uu”+到_字符串(i));
vecFilenames2.向后推_(“filename2_”+到_字符串(i));
}
互斥m;
自动f=[&](常量字符串和fn1)
{
//通过指针模拟计算其他元素
//仅适用于vector,例如deque,使用struct的容器
常量字符串&fn2=vecFilenames2[&fn1-vecFilenames1.data()];
//模拟处理(隐藏互斥不公平并生成线程
//(同时工作)
//替换为读取处理
使用名称空间chrono_文本;
此线程::睡眠时间(30ms);
//避免在锁下做任何实际工作,以从并行中获益
锁紧护板(m);
//理想情况下,不要在实际代码中从线程池执行控制台i/o

CUT应该指定C++的最新版本和哪些编译(s),因为其中一个可能的答案是并行重载,但只有C++ 17,以及标准库实现(S)。可能支持也可能不支持该重载。@下划线\d如果您能给出一个使用并行重载的小例子,我可以更改我的mingw版本,以获得新的c++17版本。
std::for_each()
您需要按照建议切换到gcc 9。虽然C++17并行算法不是唯一的方法。您可以使用英特尔TBB库或Open MP指令。实际上,gcc 9.1(查找并行性TS)你能分享一些C++ 17并行STD算法使用的例子吗?添加了一个例子。你检查了你的代码吗?我有一个错误<代码>错误:“互斥”在这个范围内没有声明< /C> >(包括并且没有任何改变)。PS见我的2个关于GCC版本的编辑。
g++.exe (MinGW.org GCC Build-2) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
#include <algorithm>
#include <chrono>
#include <iostream>
#include <execution>
#include <mutex>
#include <string>
#include <thread>

using namespace std;

vector<string> vecFilenames1;
vector<string> vecFilenames2;

int main() {
    for (int i = 1; i < 1000; i++)
    {
        vecFilenames1.push_back("filename1_" + to_string(i));
        vecFilenames2.push_back("filename2_" + to_string(i));
    }

    mutex m;

    auto f = [&](const string& fn1)
    {
        // Comupute other element via pointer arthimetics
        // Works only with vector, for, say, deque, use container of struct
        const string& fn2 = vecFilenames2[&fn1 - vecFilenames1.data()];

        // simulate processing (to hide mutex unfairness and make threads
        // working concurrently)
        // replace with read processing
        using namespace chrono_literals;
        this_thread::sleep_for(30ms);

        // avoid doing any real work under the lock to benefit from paralleling
        lock_guard<mutex> guard(m);

        // ideally don't do console i/o from thread pool in real code
        cout << "Processing " << fn1 << " & " << fn2
            << " from " << this_thread::get_id() << '\n';
    };

    for_each(execution::par, vecFilenames1.begin(), vecFilenames1.end(), f);

    return 0;
}