English 中文(简体)
范围:在档案中没有工作?
原标题:scoped_lock doesn t work on file?
  • 时间:2011-03-24 20:24:31
  •  标签:
  • boost

根据以下链接,我写了一个小的测试案例。 但它没有工作。 任何想法都值得赞赏。

Reference: http://www.cppprog.com/boost_doc/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.file_lock.file_lock_careful_iostream

#include <iostream>
#include <fstream>

#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>

using namespace std; 
using namespace boost::interprocess;

int main()
{
    ofstream file_out("fileLock.txt");
    file_lock f_lock("fileLock.txt");

    {
        scoped_lock<file_lock> e_lock(f_lock);  // it works if I comment this out
        file_out << 10;
        file_out.flush();
        file_out.close();
    }

    return 0;
}
最佳回答

坚持对六氯环己烷的测试会产生你所期望的产出。 我注意到这两项警告:

您的参考网页有这一警告:“如果你正在使用 st子:fstream/native文档,在使用卷宗上的锁时,在释放档案的所有锁点之前,不要关闭档案”。

必须指出:“如果锁定过程第二次打开档案,它就无法通过第二手处理进入指定区域,直到它锁定区域”。

至少在Windows,档案锁是每条纸,而不是每部文件。 正如我可以告诉的那样,这意味着你的方案在Windows下保证失败。

问题回答

这里是一件工作,准备在一份档案中填满根据Boost 1.44号档案。

#include "boost/format.hpp"
#include "boost/interprocess/detail/os_file_functions.hpp"

namespace ip = boost::interprocess;
namespace ipc = boost::interprocess::detail;

void fileLocking_withHandle()
{
  static const string filename = "fileLocking_withHandle.txt";  

  // Get file handle
  boost::interprocess::file_handle_t pFile = ipc::create_or_open_file(filename.c_str(), ip::read_write);
  if ((pFile == 0 || pFile == ipc::invalid_file()))
  {
    throw runtime_error(boost::str(boost::format("File Writer fail to open output file: %1%") % filename).c_str());
  }

  // Lock file
  ipc::acquire_file_lock(pFile);

  // Move writing pointer to the end of the file
  ipc::set_file_pointer(pFile, 0, ip::file_end);

  // Write in file
  ipc::write_file(pFile, (const void*)("bla"), 3);

  // Unlock file
  ipc::release_file_lock(pFile);

  // Close file
  ipc::close_file(pFile);
}




相关问题
Boost cheat sheet [closed]

I just had a hell of a project doing simple operations on C++, then when I was almost completely done with the project I found out that all this stupid tasks required just a portion of boost. This was ...

Weighted random numbers

I m trying to implement a weighted random numbers. I m currently just banging my head against the wall and cannot figure this out. In my project (Hold em hand-ranges, subjective all-in equity ...

std::locale breakage on MacOS 10.6 with LANG=en_US.UTF-8

I have a C++ application that I am porting to MacOSX (specifically, 10.6). The app makes heavy use of the C++ standard library and boost. I recently observed some breakage in the app that I m having ...

boost::thread causing small event handle leak?

I m debugging this database project. It wraps access to SQLite for a higher level application. It s designed to run asynchronously, that is, it has methods like ExecuteRequestAsync() and ...

Change command used by bjam when installing C++ boost library

I am trying to make the installation of boost library a little bit slower, because there too many calls to gcc in a short time, which increase my CPU temperature to its limit and crashes the system. ...

Should I use a const reference or a boost::shared_ptr?

I have created some C++ classes to model a Solitaire game as a learning exercise. I have classes for the SolitaireGame, a CardStack (one of the 10 piles of cards on the board ) and a Card. My ...

热门标签