Error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function

Viewed 11671

I'm having a problem trying to create a VC++ Static Library that uses C++11 standard threads.

I currently have two classes, and I am able to declare and later define a thread just fine on my starting class (which is declared last). At this stage, the code is just a socket listener which then creates an object of another class to handle each client accepted. Those child objects are supposed to create the threads I need for parallel data capture, encoding and transmission.

The problem is: If I declare a std::thread on my other class, even if exactly like I did on my start class, no matter what, I'm getting this error on build error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function [...]\vc\include\functional 1124 1

The only way I was able to workaround this error is to simply not declare a std::thread object in the latter class, which isn't possible, according to what I want it to do...

I'm using VS2013, and my sources are:

stdafx.h

#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
#include <Windows.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <thread>
#include <iostream>
#include <vector>

StreamServer.h

#pragma once
#define DEFAULT_BUFLEN 65535
#define DEFAULT_PORT "5649"

class StreamServerClient
{
public:
    bool* terminate;
    //std::thread client;     //If I comment this line out, it builds just fine.
    void DoNothing();
    StreamServerClient(SOCKET clientSock, bool* ptTerm);
    StreamServerClient();
    ~StreamServerClient();
};

class StreamServer
{
public:
    bool terminate;
    std::thread Listener;
    std::vector<StreamServerClient> clients;
    void CreateClient(SOCKET, bool*);
    void Listen();
    StreamServer();
    ~StreamServer();
};

StreamServer.cpp

#include "stdafx.h"
#include "StreamServer.h"

StreamServerClient::StreamServerClient(SOCKET clientSock, bool* ptTerm)
{
    terminate = ptTerm;
    //client = std::thread(&StreamServerClient::DoNothing, this);     //Same thing as the declaration
}

StreamServerClient::StreamServerClient()
{
    *terminate = false;
    //client = std::thread(&StreamServerClient::DoNothing, this);     //Same thing as the declaration
}

void StreamServerClient::DoNothing()
{
}

StreamServerClient::~StreamServerClient()
{
}

void StreamServer::Listen()
{
    {...}
    do {
        clients.push_back(StreamServerClient::StreamServerClient(accept(listenSock, NULL, NULL), &terminate));
        std::cout << "accepted a client!" << std::endl;
    } while (!terminate);
}

StreamServer::StreamServer()
{
    terminate = false;
    Listener = std::thread(&StreamServer::Listen, this);
    Listener.detach();
}

StreamServer::~StreamServer()
{
}
2 Answers
Related