Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::mutex and condition_variable cannot access private member inside class
    primarykey
    data
    text
    <p>I have done some googling and seen similar questions on stack overflow regarding this issue but am having trouble understanding the cause/solution to deal with it. Given the following class which is declared in ThreadPool.hpp I am getting the following 2 errors:</p> <p>Error 1 error C2248: 'std::mutex::mutex' : cannot access private member declared in class 'std::mutex' c:\users\jesse\documents\school\summer semester 2012\concurrent processing\project 2\ultra_grep v2\ultra_grep\threadpool.hpp 39 1 ultra_grep2</p> <p>Error 2 error C2248: 'std::condition_variable::condition_variable' : cannot access private member declared in class 'std::condition_variable' c:\users\jesse\documents\school\summer semester 2012\concurrent processing\project 2\ultra_grep v2\ultra_grep\threadpool.hpp 39 1 ultra_grep2</p> <pre><code>class ThreadPool { private: std::queue&lt;std::string&gt; _consoleTasks; std::queue&lt;std::tr2::sys::path&gt; _tasks; std::map&lt;std::string, std::vector&lt;GrepMatch&gt;&gt; _grepMatches; int _nThreads, _fileMatches, _totalMatches, _workingThreads; std::vector&lt;thread&gt; _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&lt;std::tr2::sys::path&gt; Tasks() { return _tasks; } }; </code></pre> <p>and the code that is implemented inside of ThreadPool.cpp:</p> <pre><code>#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 &lt; _nThreads; ++i ) { _threads.push_back( thread( &amp;ThreadPool::GrepFunc, this ) ); _tasks.push( args.root() ); ++_workingThreads; _wakeCondition.notify_one(); } for( auto&amp; t : _threads ) { t.join(); } DisplayGrepResults(); } void ThreadPool::GrepFunc() { // implement a barrier() while( !_workingThreads ) { { unique_lock&lt;mutex&gt; lk( _wakeMutex ); _wakeCondition.wait( lk ); } while( !_tasks.empty() ) { fs::path task; bool gotTask = false; { lock_guard&lt;mutex&gt; tl( _taskMutex ); if( !_tasks.empty() ) { { lock_guard&lt;mutex&gt; 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-&gt;path() ) ) { { lock_guard&lt;mutex&gt; tl( _taskMutex ); _tasks.push( dirIter-&gt;path() ); } _wakeCondition.notify_one(); } else { for( auto&amp; e : _args.extensions() ) { if( !dirIter-&gt;path().extension().compare( e ) ) { { lock_guard&lt;mutex&gt; tl( _taskMutex ); _tasks.push( dirIter-&gt;path() ); } _wakeCondition.notify_one(); //SearchFile( dirIter-&gt;path() ); } } } } } else { for( auto&amp; e : _args.extensions() ) { if( !task.extension().compare( e ) ) { if( _args.is_verbose() ) SearchFile( task ); else SearchFileVerbose( task ); } } } { lock_guard&lt;mutex&gt; tc( _threadCountMutex) ; --_workingThreads; } } } } } void ThreadPool::SearchFileVerbose( string path ) { fstream file; file.open( path ); if( !file ) { // error handling } else { { lock_guard&lt;mutex&gt; cm( _consoleMutex ); cout &lt;&lt; "\nGrepping: " &lt;&lt; path &lt;&lt; endl; int lineNumber = 1; string line; vector&lt;GrepMatch&gt; 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 &gt; 0 ) { GrepMatch match = GrepMatch( lineNumber, lineMatches, line ); matches.push_back( match ); cout &lt;&lt; "Matched " &lt;&lt; lineMatches &lt;&lt; ": " &lt;&lt; path &lt;&lt; " [" &lt;&lt; lineNumber &lt;&lt; "] " &lt;&lt; line &lt;&lt; endl; } ++lineNumber; } if( !matches.empty() ) { _grepMatches[ path ] = matches; } } } } </code></pre> <p>I am getting errors on both the mutex and the condition_variable saying that they cannot access private members declared inside the class. It is my understanding that this is referring to the copy constructor? Although I don't understand why that comes into play cause I don't see where I am trying to make a copy as they are simply private members of the class. </p> <p>As per the initial comments I only instantiate a single instance of ThreadPool and it is not copied anywhere. The only times I am trying to touch the mutexes at all are in the .cpp implementation of my class.</p> <p>Can anyone help me gain a better understanding of why this is happening?</p> <p>For those interested here is the compiler output:</p> <pre><code>1&gt;------ Build started: Project: ultra_grep2, Configuration: Debug Win32 ------ 1&gt; ultra_grep_main.cpp 1&gt; Unknown compiler version - please run the configure tests and report the results 1&gt;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&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(116) : see declaration of 'std::mutex::mutex' 1&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(107) : see declaration of 'std::mutex' 1&gt; This diagnostic occurred in the compiler generated function 'ThreadPool::ThreadPool(const ThreadPool &amp;)' 1&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(116) : see declaration of 'std::mutex::mutex' 1&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(107) : see declaration of 'std::mutex' 1&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(116) : see declaration of 'std::mutex::mutex' 1&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(107) : see declaration of 'std::mutex' 1&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(116) : see declaration of 'std::mutex::mutex' 1&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\mutex(107) : see declaration of 'std::mutex' 1&gt;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&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\condition_variable(45) : see declaration of 'std::condition_variable::condition_variable' 1&gt; c:\program files (x86)\microsoft visual studio 11.0\vc\include\condition_variable(30) : see declaration of 'std::condition_variable' 1&gt; This diagnostic occurred in the compiler generated function 'ThreadPool::ThreadPool(const ThreadPool &amp;)' 1&gt; ThreadPool.cpp 1&gt; Generating Code... ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload