#include #include #include #include using namespace std; /* simple operations on bitsets represented by unsigned int */ constexpr bool in_set(unsigned int member, unsigned int set) { return (1< lock(cout_mutex); cout << "\n+"; for (unsigned int c = 0; c < N; ++c) cout << "-"; cout << "+\n"; for (unsigned int col: positions) { cout << "|"; for (unsigned int c = 0; c < N; ++c) { if (c == col) { cout << "Q"; } else { cout << "."; } } cout << "|" << endl; } cout << "+"; for (unsigned int c = 0; c < N; ++c) cout << "-"; cout << "+\n"; } private: unsigned int row; /* current row */ /* a queen on (row, col) threatens a row, a column, and 2 diagonals; rows and columns are characterized by their number (0..n-1), the diagonals by row-col+n-1 and row+col, (n is a shorthand for the square size of the board) */ unsigned int rows, cols; /* bitmaps of [0..n-1] */ unsigned int diags1; /* bitmap of [0..2*(n-1)] used for row-col+n-1 */ unsigned int diags2; /* bitmap of [0..2*(n-1)] used for row+col */ std::list threads; /* list of forked-off threads */ std::list positions; /* columns of the queens */ }; int main() { /* spawn off the first thread with an empty board; note that we need the extra parentheses to avoid this being misinterpreted as a function declaration */ std::thread queens((Thread())); queens.join(); }