简体   繁体   English

如何动态创建具有多个对应类的多个线程来解决许多数独谜题

[英]How to dynamically create multiple threads with multiple corresponding classes for solving many sudoku puzzles

My task is to write a multi threaded program that solves a set of sudoku puzzles by dynamically determining the maximum number of threads that can run on the machine, then allocate that many threads to grab single puzzles from the file with all the sudoku puzzles.我的任务是编写一个多线程程序,通过动态确定可以在机器上运行的最大线程数来解决一组数独谜题,然后分配那么多线程来从包含所有数独谜题的文件中抓取单个谜题。

Something like: we have determined that 8 threads can run on this machine, so we'll allocate 8 threads.类似于:我们已经确定这台机器上可以运行 8 个线程,所以我们将分配 8 个线程。 Then these 8 threads take turns grabbing individual sudoku puzzles from the pile and solving them, and writing them to a new file with the solutions然后这 8 个线程轮流从堆中抓取单个数独谜题并解决它们,并将它们与解决方案一起写入一个新文件

What I currently have so far is a fully working code to grab the first puzzle, solve, and write it to the solution file.到目前为止,我所拥有的是一个完整的代码来抓取第一个谜题,解决,并将其写入解决方案文件。 But I need to make it multi threaded and have it do this for all the rest of the puzzles too.但是我需要使它成为多线程的,并且让它也为所有其余的谜题做到这一点。 I have a class that holds the sudoku puzzle data called SudokuGrid, which has the 9x9 array.我有一个名为 SudokuGrid 的类保存数独数据,它具有 9x9 数组。

I'm struggling with the concept of allocating the threads and allocating a class for each thread, I think I can generate an array to hold the threads but how do I allocate corresponding class instances?我正在为分配线程和为每个线程分配一个类的概念而苦苦挣扎,我想我可以生成一个数组来保存线程,但是如何分配相应的类实例? I believe I'll need one instance for each thread since they'll be working on their own different puzzles.我相信每个线程都需要一个实例,因为他们将处理自己不同的谜题。 I'm supposed to use std::thread for this.我应该为此使用 std::thread 。

To address your question directly (ie. not helping you with the logic of solving the puzzle, but just helping you with allocating and managing threads to methods), this is a minimalistic example of how to set up objects to do some work on different threads:为了直接解决您的问题(即不帮助您解决谜题的逻辑,而只是帮助您分配和管理线程到方法),这是一个关于如何设置对象以在不同线程上执行某些工作的简约示例:

#include <iostream>
#include <random>
#include <thread>

struct Foo
{
    int count;

    void Bar (int n)
    {
        count = 0;
    
        for (int i = 0; i < n; ++i)
            count += std::rand() % n;
    }
};

void SetUpMultiThreading (std::vector<Foo> & foo)
{
    int n = foo.size();

    std::vector<std::thread> threads(n); 
    
    // the below (2*i+5) is just some mock data input

    for (int i = 0; i < n; ++i)
        threads[i] = std::thread(&Foo::Bar, std::ref(foo[i]), 2*i+5); 
        
    // Note that without std::ref (or a custom wrapper instead)
    // then the foo element would be copied to the std::thread
    // function, so you'd lose access to the results

    for (auto & t : threads)
        t.join();
}

void CheckWork (const std::vector<Foo> & foo)
{
    for (auto & f : foo)
        std::cout << f.count << std::endl;
}

int main ()
{
    srand(time(NULL));

    const int n = 8;
    
    std::vector<Foo> foo(n);
    SetUpMultiThreading(foo);
    CheckWork(foo);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM