Boost logo

Boost Users :

Subject: [Boost-users] Boost File Locks Do not work as advertised?
From: Michael Dehmlow (dehmlowm_at_[hidden])
Date: 2010-02-12 17:14:59


I'm following the example at:
http://www.boost.org/doc/libs/1_35_0/doc/html/interprocess/synchronization_mechanisms.html

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 <boost/interprocess/sync/file_lock.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>

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_lock<boost::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--tp27570072p27570072.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net