Boost C++ Libraries: Ticket #12489: file_lock and fstream read causes "permission denied" https://svn.boost.org/trac10/ticket/12489 <p> The following code causes an exception in the std::getline. The error message received by strerror() is "permission denied". </p> <p> The exception goes away by removing the lock and unlock line. </p> <p> This issue is related to to <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/2796"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/2796</a> which focuses on writing to the filelock. A "workaround" for that issue is to unlock before flushing the stream. It is not an option to read file before locking it. </p> <p> How can this issue be fixed? Any possible workaround? </p> <p> #include "stdafx.h" </p> <p> #include &lt;iostream&gt; </p> <p> #include &lt;fstream&gt; </p> <p> #include &lt;boost/interprocess/sync/file_lock.hpp&gt; </p> <p> int main(int argc, char* argv[]) </p> <p> { </p> <blockquote> <p> std::fstream stream("lock", std::fstream::in | std::fstream::out); </p> </blockquote> <blockquote> <p> stream.exceptions(std::fstream::badbit | std::fstream::failbit); </p> </blockquote> <blockquote> <p> boost::interprocess::file_lock lock("lock"); </p> </blockquote> <blockquote> <p> lock.lock(); </p> </blockquote> <blockquote> <p> std::string content; try { </p> <blockquote> <p> std::getline(stream, content); </p> </blockquote> <p> } catch (std::exception) { </p> <blockquote> <p> auto desc = strerror(errno); </p> </blockquote> <p> } </p> </blockquote> <p> </p> <blockquote> <p> std::cout &lt;&lt; content &lt;&lt; std::endl; </p> </blockquote> <blockquote> <p> lock.unlock(); </p> </blockquote> <blockquote> <p> stream.close(); </p> </blockquote> <blockquote> <p> return 0; </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12489 Trac 1.4.3