0

I have a problem when using a vector of threads. I want to create a thread for each client in a server. So I have function called NewClient that once the connection is started, does all the necessary things for the client. This function is defined like this:

  int __cdecl NewClient(SOCKET ListenSocket, SOCKET ClientSocket,  char *recvbuf,  int recvbuflen, int iSendResult, int iResult){
//read command from client
//execute command
//answer the server
}

And the vector of threads like this:

vector<thread> thred;

So the problem comes when I create the thread in the main using the pushback:

int __cdecl main(void) {

            //things
             SOCKET ListenSocket=INVALID_SOCKET;
             SOCKET ClientSocket=INVALID_SOCKET;
             char recvbuf[DEFAULT_BUFLEN];
             int recvbuflen = DEFAULT_BUFLEN;
             int iSendResult=0;
             int iResult;
            thred.push_back(thread(NewClient,ListenSocket, ClientSocket, recvbuf, recvbuflen, iSendResult, iResult));
            thred[thred.size()-1].detach();
    }

when using(as far as I know it is used like this) :

thred.push_back(thread(NewClient,ListenSocket, ClientSocket, recvbuf, recvbuflen, iSendResult, iResult));

I get two errors, underlining thread:

Error 2 error C2661: 'std::thread::thread' : ninguna función sobrecargada acepta 7 argumentos D:\Descargas\Baaer 1.0.22\Baaer 1.0.22\Server\Server\Source_s.cpp 170 1 Server

3 IntelliSense: ninguna instancia del constructor "std::thread::thread" coincide con la lista de argumentos los tipos de argumento son: (int __cdecl (SOCKET ListenSocket, SOCKET ClientSocket, char *recvbuf, int recvbuflen, int iSendResult, int iResult), SOCKET, SOCKET, char [512], int, int, int) d:\Descargas\Baaer 1.0.22\Baaer 1.0.22\Server\Server\Source_s.cpp 170 19 Server

So how do I solve it?

19mike95
  • 506
  • 2
  • 4
  • 19

1 Answers1

1

Simplified example:

Live On Coliru

#include <thread>
#include <vector>
#include <string>
#include <iostream>

void foo(int, double, std::string, bool) {
    std::cout << "foo\n";
}

int main() {
    std::vector<std::thread> threads;
    threads.reserve(10);

    threads.emplace_back(foo, 42, 3.14,  "string", false);
    threads.emplace_back(foo, 52, 13.14, "data",   true);

    for (auto& th : threads)
        if (th.joinable()) 
            th.join();
}

Prints

foo
foo
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks for answering, I tried this but gave C2660 error. And says that: 'std::vector<_Ty>::emplace_back' the function doesn't accept 7 arguments – 19mike95 Apr 23 '16 at 16:45