数独算法中的多线程 C++

multithreading in a sudoku algorithm c++

这是一个多线程程序,它读取一个 9x9 的矩阵并验证它是否是一个有效的数独。我正在读取输入并将其存储在二维数组中。对于每一行,我试图为该行创建一个线程以确定它是否包含数字 1 到 9。总共 9 个线程用于检查行。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <thread>

const int ROW = 9;
const int COLUMN = 9;

std::thread t[27];

using namespace std;

void DisplayBoard (int board[ROW][COLUMN]);
bool ReadBoard (char* filename, int board[ROW][COLUMN]);
void CheckRows(vector <int> valid_ints, vector<int> board_row, bool &check_status);
vector <int> stripList (int tmp, vector <int> valid_ints);

int main(int argc, char * argv[]) {

  int board[ROW][COLUMN];

  // If the user didn't provide a filename command line argument,
  // print an error and exit.
  if (argc <= 1)
    {
      cout << "Usage: " << argv[0] << " <Filename>" << endl;
      //exit(1);
    }

  char *filename = argv[1];

  if (ReadBoard(filename, board)){
    DisplayBoard(board);

    vector <int> valid_ints;
    // Valid numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9
    for(int i = 1; i <= 9; i++)
      {
        valid_ints.push_back(i);
      }

    vector <int> board_row;

    bool check_status = true;


    //iterate through the board
    for (int i = 0; i < ROW; i++){
      for (int j = 0; j < COLUMN; j++){

        //pushed each number from row
        board_row.push_back(board[i][j]);

      }

      //threading 
      //***** ERROR *****
      t[i] = thread(CheckRows, valid_ints, board_row, check_status);

      // if valid ints aren't all striped, error in the row
      if (check_status == false){
        cout << endl;
        cout << "Invalid Row: " << i+1 << endl;
        cout << "The input is not a valid Sudoku." << endl;
        cout << endl;
        exit(1);
      }
      else {
        board_row.clear();
        valid_ints.clear();

        // Valid numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9
        for(int z = 1; z <= 9; z++)
        {
            valid_ints.push_back(z);
        }
      }
    }

这是我的检查行函数:

void CheckRows(vector <int> valid_ints, vector<int> board_row, bool &check_status){

  for (int i = 0; i < board_row.size(); i++){
    for (int j = 0; j < valid_ints.size(); j++){
      if (board_row[i] == valid_ints[j])
        //removes the board value and updates valid ints until all are found (valid)
        valid_ints = stripList(board_row[i], valid_ints);
    }
  }
  // invalid row
  if (valid_ints.size() != 0)
    check_status = false;

}

我目前遇到的错误:

In file included from /usr/local/include/c++/5.1.0/thread:39:0,
                 from sudoku.cpp:5:
/usr/local/include/c++/5.1.0/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::vector<int>, std::vector<int>, bool*))(std::vector<int>, std::vector<int>, bool&)>’:
/usr/local/include/c++/5.1.0/thread:137:59:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::vector<int>, std::vector<int>, bool&); _Args = {std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&, bool*}]’
sudoku.cpp:65:68:   required from here
/usr/local/include/c++/5.1.0/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<int>, std::vector<int>, bool*))(std::vector<int>, std::vector<int>, bool&)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/local/include/c++/5.1.0/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<int>, std::vector<int>, bool*))(std::vector<int>, std::vector<int>, bool&)>’
         _M_invoke(_Index_tuple<_Indices...>)

我不确定在创建线程时我做错了什么。请帮忙。提前致谢。

尝试将行更改为:

t[i] = thread(CheckRows, valid_ints, board_row, std::ref(check_status));

http://en.cppreference.com/w/cpp/thread/thread/thread