2

I have been writing a sudoku solver in c++ on Xcode 7. I managed to write a successful solver using a backtracking algorithm.

Now i'm trying to parallelize whatever functions inside my solver so that I can to speed up the solving algorithm. I have 3 functions that are in charge of checking if the current number trying to be inserted into the grid exists in the row, column or box. (standard sudoku rules). Since these are mutually exclusive operations I want to parallelize them.

I know it's overkill to multithread this program, but the goal is more to learn multithreading rather than speed up my solver algorithm.

This is what I've got so far. I've included the standard c++11 thread library. Using default Xcode 7 build settings.

The error I get says that I'm attempting to use a deleted function which pops up when I hit the "Build and Run" button on Xcode. Xcode's intellisense does not complain bout my code. I don't understand this. Please help.

#include <thread>

....

typedef uint8_t byte;
typedef uint16_t dbyte;

....

bool sudokuGame::check(byte num, byte row, byte col)
{
    setBoxFlag(true);
    setColFlag(true);
    setRowFlag(true);

    std::thread t1{&sudokuGame::checkRow, num, row};
    std::thread t2{&sudokuGame::checkColumn,num,col};
    std::thread t3{&sudokuGame::checkBox,num,row,col};

    t1.join();
    t2.join();
    t3.join();

    return (getBoxFlag() && getRowFlag() && getColFlag()); 
}

Somewhere inside "thread" where the "attempting to use a deleted function" ERROR pops up.

...

__thread_execute(tuple<_Fp, _Args...>& __t, __tuple_indices<_Indices...>)
{
    __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
}

...

My build settings looks like this

xcode build settings screen capture

Anoop
  • 77
  • 1
  • 8
  • Are the `checkXXX` functions marked as `static`? If not, then you need the thread to call these functions using a specific instance of the `sudokuGame` class. – Some programmer dude Nov 25 '15 at 07:12
  • And *how* do you define the type-alias `byte`? It can't be both at the same time. – Some programmer dude Nov 25 '15 at 07:13
  • And finally, why are you trying to create the same threads *twice*? What you're doing is like doing `int a = 5; a = 5;`, the assignment makes no sense (incidentally, "fixing" this will fix your problem). – Some programmer dude Nov 25 '15 at 07:14
  • 1. They are non-static member functions, however Xcode complains that I "Must explicitly qualify name of member function when taking it's address" and without the &, it complains that "reference to a non-static member must be called" 2. That's a typo. It should be fixed soon 3. That's a typo as well. Was trying different things, had them commented out. Should be fixed soon. – Anoop Nov 25 '15 at 07:37
  • And how is it possible to call one member function of a class from another member function using an instance? Where would I declare this instance? – Anoop Nov 25 '15 at 07:47

1 Answers1

4

To create a thread using non-static member functions, you need to provide the instance the member function should use, the this variable inside the thread function. This is done by passing the instance as the first argument to the thread function:

std::thread t1{&sudokuGame::checkRow, this, num, row};
//                                    ^^^^
//             Note the use of `this` here

Note that you don't need to change the thread function, it's all handled by the standard library and the compiler.

Then for another problem: The thread assignments like

t1=std::thread(&sudokuGame::checkRow, num, row);

They don't make any sense. You have already created the thread objects, initialized them, and got the thread running. And because the threads are already running you can't assign to them. See e.g this reference for the overloaded thread assignment operator.

The compiler error you get is because of the first problem, that you don't pass an instance when creating the threads.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Awesome! The addition of 'this' as another argument fixed it. I did not even have to modify the member function definition or it's argument list. Thank you so much! – Anoop Nov 25 '15 at 07:53
  • As for thread assignment. That part was actually commented out, I accidentally uncommented it while posting it here. But thank you anyways for explaining what would have gone wrong. – Anoop Nov 25 '15 at 07:54