I'm following the example at:
http://www.boost.org/doc/libs/1_35_0/doc/html/interprocess/synchronization_m...
for locking and writing to a file, but when a flush is called either
implicitly due to a full buffer, or an explicit flush no data is written and
the fstream bad() call returns true.
If I write a small enough amount of data and don't explicitly flush it works
because I unlock the file then call close (where an implicit flush occurs).
However, if a flush occurs at any time the file lock is locked no data is
written and bad() return true.
[START OF CODE]
#include <fstream>
#include <iostream>
#include
#include
int main()
{
std::string tocFile("C:\\temp\\myfile.bin");
std::fstream file;
file.open(tocFile.c_str(), std::ios::out | std::ios::binary |
std::ios::trunc);
if (!file.is_open() || file.bad())
{
std::cout << "Open failed" << std::endl;
exit(-1);
}
boost::interprocess::file_lock f_lock(tocFile.c_str());
{
boost::interprocess::scoped_lockboost::interprocess::file_lock
e_lock(f_lock);
file.write(tocFile.c_str(),tocFile.length());
if (!file.is_open() || file.bad())
{
std::cout << "Write failed" << std::endl;
exit(-1);
}
file.flush();
if (!file.is_open() || file.bad())
{
std::cout << "Flush failed" << std::endl;
exit(-1);
}
}
file.close();
return 0;
}
[END OF CODE]
--
View this message in context: http://old.nabble.com/Boost-File-Locks-Do-not-work-as-advertised--tp27570072...
Sent from the Boost - Users mailing list archive at Nabble.com.