Threading class methods [duplicate]

  c++, multithreading

I’m having problems when trying to run threads with class methods.
Ex:

#include "p2p/p2pServer.h"
#include <stdio.h>
#include <stdlib.h>
#include <thread>


int main() {
    p2pServer server;
    std::thread listener(server.listenPeers());
    std::thread connector(server.connectPeers());
    
    listener.join();
    connector.join();

}

p2pServer is a class that has two functions: listenPeers and connectPeers, twice are int functions. My objective is to run that two functions at the same time using threads. But I’m getting all time this error when compiling: C2672 "invoke": No matching overloaded function found.

I have seen in some forums that thread constructor takes function without "()" like:

std::thread listener(server.listenPeers)

But if I try to create a thread without "()" I got another error that says that a pointer to a linked function can only be used to call the function.

Does anyone know how to solve it, or other form to create threads for class methods?
Thanks in advance for your answers.

Source: Windows Questions C++

LEAVE A COMMENT