简体   繁体   中英

Visual Studio 2012 error C2248 in std::thread

I have a problem with the Visual Studio 2012 implementation of the std::thread class.

Error C2248: "std::thread::thread": cannot access private member declared in class std::thread
    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0 line: 606

A.hpp:

class A{ 
    public:
        A();
        ~A();


    private:
        vector<thread> listOfThreads;       
        int numberOfProcessorCores;
        int startUpWorkerThreads();
};

A.cpp:

    int A::startUpWorkerThreads(){
        if(numberOfProcessorCores <= 0) return 2; //Keine Angabe zur Anzahl der Prozessorkerne
        if(listOfThreads.size() > 0) return 3; //Bereits initialisiertdefiniert

        for(int i = 0; i < numberOfProcessorCores; i ++){
            thread newThread(&TaskManagement::TaskManager::queueWorker);            
            listOfThreads.push_back(newThread);
        }

        return 0;
    }

This is the part of my programm where the thread-class is used.

Does anybody know why this error occures?

The error is telling you that an operation is attempting to call std::thread 's copy constructor or assignment operator, both of which are deleted or private. As an alternative, you can "move" a thread into the vector by pushing a temporary like this:

listOfThreads.push_back(thread(&TaskManagement::TaskManager::queueWorker));

otherwise, you could call std::move on your thread object, which leaves you with a thread object in the same state as a default constructed one (thanks to @JonathanWakely for pointing that out in comments). In your case, there is no reason to create a thread and explicitly move it.

std::thread没有复制构造函数,它是执行push_back所需的,并且可能用于其他vector操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM