我已经进行了一些谷歌搜索,并在堆栈溢出中看到了与此问题类似的问题,但是在理解导致该问题的原因/解决方案时遇到了麻烦。给定以下在ThreadPool.hpp中声明的类,我得到以下2个错误:

错误1错误C2248:'std::mutex::mutex':无法访问在类'std::mutex'中声明的私有(private)成员c:\users\jesse\documents\school\summer学期2012\并发处理\project 2\ultra_grep v2\ultra_grep\threadpool.hpp 39 1 ultra_grep2

错误2错误C2248:'std::condition_variable::condition_variable':无法访问在类'std::condition_variable'中声明的私有(private)成员c:\users\jesse\documents\school\summer 2012年学期\并发处理\项目2 v2\ultra_grep\threadpool.hpp 39 1 ultra_grep2

class ThreadPool
{
private:
std::queue<std::string> _consoleTasks;
std::queue<std::tr2::sys::path> _tasks;
std::map<std::string, std::vector<GrepMatch>> _grepMatches;
int _nThreads, _fileMatches, _totalMatches, _workingThreads;
std::vector<thread> _threads;
Arguments _args;
std::mutex _taskMutex, _wakeMutex, _consoleMutex, _threadCountMutex;
std::condition_variable _wakeCondition;

public:
ThreadPool( int threads, Arguments args );
~ThreadPool() {};
void GrepFunc();
void ConsoleFunc();
void SearchFile( std::string );
void SearchFileVerbose( std::string );
void DisplayGrepResults();

queue<std::tr2::sys::path> Tasks() { return _tasks; }

};

以及在ThreadPool.cpp内部实现的代码:
#include "ThreadPool.hpp"

using namespace std;
namespace fs = std::tr2::sys;

ThreadPool::ThreadPool( int threads, Arguments args )
: _nThreads( threads ), _args( args ), _fileMatches( 0 ), _totalMatches( 0 ), _workingThreads( 0 )
{

for( int i = 0; i < _nThreads; ++i )
{
        _threads.push_back( thread( &ThreadPool::GrepFunc, this ) );
        _tasks.push( args.root() );
        ++_workingThreads;
        _wakeCondition.notify_one();
}

for( auto& t : _threads )
{
    t.join();
}

DisplayGrepResults();
}

void ThreadPool::GrepFunc()
{
// implement a barrier()

while( !_workingThreads )
{
    { unique_lock<mutex> lk( _wakeMutex );
    _wakeCondition.wait( lk ); }

    while( !_tasks.empty() )
    {
        fs::path task;
        bool gotTask = false;
        {
            lock_guard<mutex> tl( _taskMutex );
            if( !_tasks.empty() )
            {
                { lock_guard<mutex> tc( _threadCountMutex );
                ++_workingThreads; }
                task = _tasks.front();
                _tasks.pop();
                gotTask = true;
            }
        }

        if( gotTask )
        {
            if( fs::is_directory( task ) )
            {
                for( fs::directory_iterator dirIter( task ), endIter; dirIter != endIter; ++dirIter )
                {
                    if( fs::is_directory( dirIter->path() ) )
                    {
                        { lock_guard<mutex> tl( _taskMutex );
                        _tasks.push( dirIter->path() ); }
                        _wakeCondition.notify_one();
                    }
                    else
                    {
                        for( auto& e : _args.extensions() )
                        {
                            if( !dirIter->path().extension().compare( e ) )
                            {
                                { lock_guard<mutex> tl( _taskMutex );
                                _tasks.push( dirIter->path() ); }
                                _wakeCondition.notify_one();
                                //SearchFile( dirIter->path() );
                            }
                        }
                    }
                }
            }
            else
            {
                for( auto& e : _args.extensions() )
                {
                    if( !task.extension().compare( e ) )
                    {
                        if( _args.is_verbose() )
                            SearchFile( task );
                        else
                            SearchFileVerbose( task );
                    }
                }
            }

            { lock_guard<mutex> tc( _threadCountMutex) ;
            --_workingThreads; }
        }
    }
}
}

void ThreadPool::SearchFileVerbose( string path )
{
fstream file;
file.open( path );

if( !file )
{
    // error handling
}
else
{
    {
        lock_guard<mutex> cm( _consoleMutex );
        cout << "\nGrepping: " << path << endl;

        int lineNumber = 1;
        string line;
        vector<GrepMatch> matches;
        while( getline( file, line ) )
        {
            int lineMatches = 0;
            sregex_token_iterator end;
            for (sregex_token_iterator i(line.cbegin(), line.cend(), _args.regular_expression() );
                i != end;
                ++i)
            {
                ++lineMatches;
            }

            if( lineMatches > 0 )
            {
                GrepMatch match = GrepMatch( lineNumber, lineMatches, line );
                matches.push_back( match );
                cout << "Matched " << lineMatches << ": " << path << " [" << lineNumber << "] " << line << endl;
            }

            ++lineNumber;
        }

        if( !matches.empty() )
        {
            _grepMatches[ path ] = matches;
        }
    }
}
}

我在互斥体和condition_variable上都收到错误消息,说它们无法访问在类内部声明的私有(private)成员。据我了解,这是指复制构造函数?尽管我不明白为什么会这样做,但我却看不到我要在哪里复制,因为他们只是类的私有(private)成员。

根据最初的注释,我仅实例化ThreadPool的单个实例,并且不会将其复制到任何地方。我唯一想碰互斥的唯一机会是在我的类的.cpp实现中。

谁能帮助我更好地了解为什么会发生这种情况?

对于那些感兴趣的人是编译器输出:
1>------ Build started: Project: ultra_grep2, Configuration: Debug Win32 ------
1>  ultra_grep_main.cpp
1>  Unknown compiler version - please run the configure tests and report the results
1>c:\users\jesse\documents\school\summer semester 2012\concurrent processing\project 2\ultra_grep v2\ultra_grep\threadpool.hpp(39): error C2248: 'std::mutex::mutex' : cannot access private member declared in class 'std::mutex'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(116) : see declaration of 'std::mutex::mutex'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(107) : see declaration of 'std::mutex'
1>          This diagnostic occurred in the compiler generated function 'ThreadPool::ThreadPool(const ThreadPool &)'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(116) : see declaration of 'std::mutex::mutex'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(107) : see declaration of 'std::mutex'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(116) : see declaration of 'std::mutex::mutex'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(107) : see declaration of 'std::mutex'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(116) : see declaration of 'std::mutex::mutex'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(107) : see declaration of 'std::mutex'
1>c:\users\jesse\documents\school\summer semester 2012\concurrent processing\project 2\ultra_grep v2\ultra_grep\threadpool.hpp(39): error C2248: 'std::condition_variable::condition_variable' : cannot access private member declared in class 'std::condition_variable'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\condition_variable(45) : see declaration of 'std::condition_variable::condition_variable'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\condition_variable(30) : see declaration of 'std::condition_variable'
1>          This diagnostic occurred in the compiler generated function 'ThreadPool::ThreadPool(const ThreadPool &)'
1>  ThreadPool.cpp
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

该错误在某处:



使用ThreadPool的拷贝构造函数的位置:



实际上,令我惊讶的是,编译器没有指向隐式生成拷贝构造函数的源文件中的哪一行。

在注释中,您询问复制构造函数的外观。一个更好的问题是制作ThreadPool对象的拷贝是否有意义,答案是通常没有。为了改善错误消息,您可以将复制构造函数标记为deleted,这样(希望如此),编译器将针对需要复制的位置生成更好的错误报告:

class ThreadPool {
   ThreadPool( ThreadPool const & ) = delete;
// ...
};

如果您的编译器不支持此C++ 11功能,则替代方法是声明复制构造函数而不定义它。这将在复制构造函数的使用位置触发访问错误。

关于c++ - std::mutex和condition_variable无法访问类内的私有(private)成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11996916/

10-11 22:47