1

I'm having this error: error c2064: term does not evaluate to a function taking 0 arguments. The thing is the function takes 0 arguments and I call 0 arguments, and I don't understand what's wrong.

    RWLock* rwl = new RWLock();
    std::thread t1(&RWLock::read);

That's the call to the function. And this is the function:

    void read();

Does somebody know what the problem is? Thanks in advance!

Alon Pe'er
  • 43
  • 1
  • 6

2 Answers2

8

All non-static member functions have a hidden argument, that becomes the this pointer in the member function If you want to use a non-static member function as a thread, you have to provide this hidden argument when starting the thread:

std::thread t1(&RWLock::read, rwl);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

I am pretty sure, that read() is not a static function. That is, you declared it as:

void read()

instead of:

static void read()

Non-static member function takes always one additional argument - implicit this pointer. Make read() static and it should work.

If read() cannot be static, pass an additional argument to std::thread - a pointer to object, that read() will work on. In this case it should be rwl:

RWLock* rwl = new RWLock();
std::thread t1(&RWLock::read, rwl);
Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49