2015-10-07 19 views
0

這是一個多線程的程序,讀取9x9的矩陣,並驗證它是否是一個有效的數獨與否。我正在讀取並將輸入存儲在一個二維數組中。對於每一行,我正在嘗試爲該行創建一個線程,以確定它是否包含數字1到9.共有9個用於檢查行的線程。多線程在一個數獨算法C++

#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...>) 

我不知道創建線程的時候我在做什麼錯。請幫忙。提前致謝。

+1

多線程同步不...你哪裏不等待結果計算測試結果...修復已經失蹤' const'/reference之前嘗試線程這不是一個容易的領域。 – Jarod42

+0

另外請注意,只拋出固定數量的線程並不一定會加速性能(但甚至可能會使情況變得更糟)。以某種方式使用可用CPU核心來平衡線程數量。 –

回答

0

嘗試改變行:

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

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

+1

這不是唯一的問題... – Jarod42

+0

我有這種懷疑,但我沒有g ++ 5.1編譯器,方便atm。 –

+1

有一些在線編譯器爲[ideone](https://ideone.com/)或[coliru](http://coliru.stacked-crooked.com/),... – Jarod42