【问题标题】:Multithreading Reuse with Objects in While loop在 While 循环中对对象进行多线程重用
【发布时间】:2020-03-31 20:10:34
【问题描述】:

我是多线程新手,已经开始遇到问题。 我想在一个类函数上实现 10 个线程重用,它的父类在 while 循环中有 4 个函数,但它给了我像 "error C3867: 'std::thread::join 这样的错误':非标准语法;使用 '&' 创建指向成员的指针" 和 "std::system_error at memory location" 由于我不知道我解释得有多好,我将向您展示我想要实现的伪代码: 主.cpp

Game newGame;
/* Create Players */
Population pop(NumberOfPlayers);

/* Create Enemies */
std::vector<Enemies> enemy;
for (int i = 0; i < NumberOfEnemies; i++) {
    Enemies e(i);
    enemy.push_back(e);
}
while (!pop.hasFinished) {
        std::thread threads[NUM_THREADS];
        for (int i = 0; i < NUM_THREADS; i++) {
            threads[i] = std::thread(std::bind(&Population::populationMove, &pop, (i * NumberOfPlayers / NUM_THREADS)));
            //std::cerr << "In Thread " << i << "\n";
        }
        for (int i = 0; i < NUM_THREADS; i++) {
            if(threads[i].joinable)
                threads[i].join;
            //std::cerr << "In Thread " << i << "\n";
        }
        for (int i = 0; i < NumberOfEnemies; i++) {
            enemy[i].movement();
            enemy[i].checkIfKills();
        }
        enemymoves++;
        if (pop.checkIfAllDead()) {
            pop.calculateBest();
            pop.createNewPopulation();
            for (int i = 0; i < NumberOfEnemies; i++) {
                if ((pop.gen - 1) % 5 == 0) {
                    enemy[i].save();
                }
                enemy[i].reset();
            }
        }
}


这是populationMove()的伪代码

 Population::populationMove(int threadStartIndex) {

    for (int i = threadStartIndex; i < threadStartIndex + (player.size() / NUM_THREADS); i++) {
        mtx.lock();
        player[i].move();
        mtx.unlock();

    }

【问题讨论】:

  • 也许尝试将std::thread threads[N_THREADS]; 放在while 循环中而不是放在外面?
  • 在询问构建错误或消息时,请包含一个实际的minimal reproducible example,我们可以使用它来复制错误。并且始终包含该示例中的实际错误消息,完整且完整地复制粘贴(并且复制粘贴为文本,而不是图像)。并在代码中添加 cmets 以显示错误所在。另外请花一些时间阅读how to ask good questionsthis question checklist
  • "错误 C3867: 'std::thread::join': 非标准语法;使用 '&' 创建指向成员的指针"
  • 有一个错字,开头是:= std::thread(&amp;Class::classFunction, &amp;classObject, threadindex); -- 不应该有大括号。
  • 首先将 fullcomplete 错误输出复制粘贴到问题本身。然后显示导致错误的实际代码,而您没有。

标签: c++ multithreading


【解决方案1】:

好的,我想这是你的第一个问题。

std::thread threads[N_THREADS]; // array of threads

这将构造 10 个线程,而不是 10 个线程的 SPACE。我不是绝对肯定的,所以我这样测试:

#include <iostream>

using namespace std;

class Foo {
public:
    Foo() { cout << "Foo constructor.\n"; }
};

int main(int, char **) {
    cout << "Allocate array.\n";

    Foo array[10];

    cout << "Loop.\n";
    for (int i = 0; i < 10; ++i) {
        array[i] = Foo();
    }

    cout << "Done.\n";
}

这显示了我的第一个提示的输出,然后是 10 个“Foo 构造函数”消息,然后是我的第二个“循环”提示,然后是另外 10 个“Foo 构造函数”消息。

我认为您可能需要将其设为线程指针数组,然后使用 new,如下所示:

std::thread * thread[N_THREADS];

for (int i = 0; i < NUM_THREADS; i++) {
    threads[i] = new std::thread(std::bind(&Population::populationMove, &pop, (i * NumberOfPlayers / NUM_THREADS)));
}

接下来,thread.join 是一个方法:

    for (int i = 0; i < NUM_THREADS; i++) {
        if(threads[i].joinable())
            threads[i].join();
        //std::cerr << "In Thread " << i << "\n";
    }

您在该代码中的两个地方缺少 ()。

【讨论】:

  • 谢谢,2小时没看到牙套了,哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
  • 2019-10-04
相关资源
最近更新 更多