首页 \ 问答 \ c ++多进程写入同一个文件 - 进程间互斥?(c++ multiple processes writing to the same file - Interprocess mutex?)

c ++多进程写入同一个文件 - 进程间互斥?(c++ multiple processes writing to the same file - Interprocess mutex?)

我的问题是:从多个进程写入文件的最佳方式(或至少是一种有效的方法)是什么?

注意:我正在使用c ++ 11,我希望这可以在任何平台上运行(即纯c ++代码)。

我已经做了一些研究,下面是我所得出的结论:

  1. 在我的进程中,我有多个线程。 这很容易在每个进程中使用互斥来处理对文件的访问。
  2. 一个c ++ / c ++ 11互斥体或条件变量不能用于序列化进程之间。
  3. 我需要某种外部信号量/锁定文件来充当“互斥体”......但我不确定如何去做这件事。

我看到应用程序在使用时会使用诸如创建“.lock”文件之类的东西。 但是对于多次快速访问,似乎这可能不起作用(即,在一个进程已经决定文件不存在之后,另一个进程可以创建它,然后第一个进程也会尝试创建它),因为测试和创建文件的操作是不是原子的。

注意:每个进程一次总是写一整行。 我曾认为这可能足以使操作成为“原子”(因为整行将在下一行之前被缓冲),但似乎并非如此(除非我的代码错误),因为我(很少)得到一条损坏的线。 这里是我如何写一个代码片段(如果它是相关的):

// in c'tor
m_osFile.open("test.txt", std::fstream::out | std::fstream::app)

// in write func (std::string data)
osFile << data<< std::endl;

这肯定是一个常见问题,但我还没有找到一个可行的解决方案。 任何代码片段都会受到欢迎。


My question is this: what is the best way (or at least an effective way) to write to a file from multiple processes?

Note: I am using c++11 and I want this to run on any platform (i.e. pure c++ code only).

I have done some research and here is what I have concluded:

  1. In my processes I have multiple threads. This is easily handled within each process using a mutex to serialise access to the file.
  2. A c++/c++11 mutex or conditional variable cannot be used to serialise between processes.
  3. I need some sort of external semaphore / lock file to act as a "mutex"... but I am not sure how to go about doing this.

I have seen applications use things like creating a ".lock" file when in use. But for multiple rapid access it seems like this may not work (i.e. after one process has decided the file does not exist another could create it and then the first process will also try to create it) because the operation to test and create the file is not atomic.

Note: Each process always writes one entire line at a time. I had thought that this might be enough to make the operation "atomic" (in that a whole line would get buffered before the next one), but this does not appear to be the case (unless I have my code wrong) since I (rarely) get a mangled line. Here is a code snippet of how I am doing a write (in case it is relevant):

// in c'tor
m_osFile.open("test.txt", std::fstream::out | std::fstream::app)

// in write func (std::string data)
osFile << data<< std::endl;

This must be a common-ish issue, but I have not yet found a workable solution to it. Any code snippets would be welcome.


原文:https://stackoverflow.com/questions/49381583
更新时间:2022-09-23 17:09

最满意答案

你可以像下面的代码那样做。 需要进行一些调整以获得您想要的确切结果(如图像),但我相信它会让您顺利进行。

我为'进步'栏添加了一些jQuery。 通过使用data-progress (使用百分比!!!),您可以定义进度的程度。

作为替代方案,您可以将data-progress="90%"更改为style="width: 90%" ,这使其成为100%HTML / CSS。

$(function() {
	$('.progress>div').each(function() {
  	$(this).css('width', $(this).data('progress') );
  });
});
* {
  box-sizing: border-box;
}
body {
  background: white;
}
.project {
  width: 400px;
  margin-bottom: 1em;
}
  .project > div {
    display: inline-block;
    margin: 0 -5px 0 0;
    vertical-align: middle;
  }
.task {
  width: 2em;
  height: 2em;
  border: .4em solid #E4E4E7;
  background: #E4E4E7;
  border-radius: 100%;
}
.progress {
  width: calc( 50% - 3em);
  height: .6em;
  padding: .2em 0;
  background: #E4E4E7;
  position: relative;
}
  .progress>div {
    height: .2em;
    left: -.4em;
    right: -.4em;
    position: absolute;
  }
.pending { background: #F8AC59; }
.running { background: #1C84C6; }
.passed { background: #1AB394; }
.failed { background: #ED5565; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="project">
  <div class="task pending"></div>
  <div class="progress"><div> </div> </div>
  <div class="task"></div>
  <div class="progress"><div> </div></div>
  <div class="task"></div>
</div>

<div class="project">
  <div class="task running"></div>
  <div class="progress"><div class="running" data-progress="90%"> </div> </div>
  <div class="task"></div>
  <div class="progress"><div> </div></div>
  <div class="task"></div>
</div>

<div class="project">
  <div class="task passed"></div>
  <div class="progress"><div class="passed"> </div> </div>
  <div class="task passed"></div>
  <div class="progress"><div class="passed"> </div></div>
  <div class="task passed"></div>
</div>

<div class="project">
  <div class="task failed"></div>
  <div class="progress"><div> </div> </div>
  <div class="task"></div>
  <div class="progress"><div> </div></div>
  <div class="task"></div>
</div>

<div class="project">
  <div class="task passed"></div>
  <div class="progress"><div class="passed"> </div> </div>
  <div class="task passed"></div>
  <div class="progress"><div class="passed"> </div></div>
  <div class="task passed"></div>
</div>

<div class="project">
  <div class="task failed"></div>
  <div class="progress"><div class="failed"> </div> </div>
  <div class="task failed"></div>
  <div class="progress"><div> </div></div>
  <div class="task"></div>
</div>


You could do it like the following code. It will need some tweaking to get the exact result you want (like images), but I believe it will get you on your way.

I've added a little bit of jQuery for 'progress' bars. By using the data-progress (use percentage!!!) you can define how far the progress is.

As an alternative to the you could change the data-progress="90%" to style="width: 90%" which makes it 100% HTML/CSS.

$(function() {
	$('.progress>div').each(function() {
  	$(this).css('width', $(this).data('progress') );
  });
});
* {
  box-sizing: border-box;
}
body {
  background: white;
}
.project {
  width: 400px;
  margin-bottom: 1em;
}
  .project > div {
    display: inline-block;
    margin: 0 -5px 0 0;
    vertical-align: middle;
  }
.task {
  width: 2em;
  height: 2em;
  border: .4em solid #E4E4E7;
  background: #E4E4E7;
  border-radius: 100%;
}
.progress {
  width: calc( 50% - 3em);
  height: .6em;
  padding: .2em 0;
  background: #E4E4E7;
  position: relative;
}
  .progress>div {
    height: .2em;
    left: -.4em;
    right: -.4em;
    position: absolute;
  }
.pending { background: #F8AC59; }
.running { background: #1C84C6; }
.passed { background: #1AB394; }
.failed { background: #ED5565; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="project">
  <div class="task pending"></div>
  <div class="progress"><div> </div> </div>
  <div class="task"></div>
  <div class="progress"><div> </div></div>
  <div class="task"></div>
</div>

<div class="project">
  <div class="task running"></div>
  <div class="progress"><div class="running" data-progress="90%"> </div> </div>
  <div class="task"></div>
  <div class="progress"><div> </div></div>
  <div class="task"></div>
</div>

<div class="project">
  <div class="task passed"></div>
  <div class="progress"><div class="passed"> </div> </div>
  <div class="task passed"></div>
  <div class="progress"><div class="passed"> </div></div>
  <div class="task passed"></div>
</div>

<div class="project">
  <div class="task failed"></div>
  <div class="progress"><div> </div> </div>
  <div class="task"></div>
  <div class="progress"><div> </div></div>
  <div class="task"></div>
</div>

<div class="project">
  <div class="task passed"></div>
  <div class="progress"><div class="passed"> </div> </div>
  <div class="task passed"></div>
  <div class="progress"><div class="passed"> </div></div>
  <div class="task passed"></div>
</div>

<div class="project">
  <div class="task failed"></div>
  <div class="progress"><div class="failed"> </div> </div>
  <div class="task failed"></div>
  <div class="progress"><div> </div></div>
  <div class="task"></div>
</div>

相关问答

更多
  • 这听起来像你想要的不是一个CSS框架,而只是一个简单的自定义样式表。 不要阻止你寻找已经存在的东西,但你最好自己写。 也许从许多现有的reset.css 。 没有能力通过类名钩住(或离开)的默认样式不是很灵活。 你真的不应该避免上课,你应该利用他们。 没有框架会知道你的HTML元素在哪里和如何嵌套,或者它们根据标签名称应该是什么样子,这就是我们通常使用类的原因之一。 如果您密切关注新的HTML5元素的语义,您会发现该header并不总是意味着“带有徽标的页面顶部”, footer不仅仅是整个页面底部的元素, ...
  • 我远离w3schools - 他们不隶属于W3C。 有关详细信息,请访问http://w3fools.com/ 。 一些更好,更可靠的资源是: http://dev.opera.com/articles/view/1-introduction-to-the-web-standards-cur/#toc (面向初学者 - 请参阅HTML5部分) & https://developer.mozilla.org/en-US/docs/HTML/HTML5 (中级更多) I'd stay away from w3s ...
  • 你可以像下面的代码那样做。 需要进行一些调整以获得您想要的确切结果(如图像),但我相信它会让您顺利进行。 我为'进步'栏添加了一些jQuery。 通过使用data-progress (使用百分比!!!),您可以定义进度的程度。 作为替代方案,您可以将data-progress="90%"更改为style="width: 90%" ,这使其成为100%HTML / CSS。 $(function() { $('.progress>div').each(function() { $(this).cs ...
  • 把它想象成一所学校 多个学生的课程(多个元素) 单个学生的ID (单个元素) Think of it as a school class for multiple students (multiple elements) id for a single student (single element)
  • 获得基础知识的一个良好开端是阅读Mark Pilgrim的Dive Into HTML5 。 您甚至可以在购买之前尝试,因为它是免费的。 另一个建议是Pro HTML 5编程:用于更丰富的Internet应用程序开发的强大API (非免费)。 对于最全面的阅读,我推荐HTML5规范 ,其中包含所有细节。 Head First HTML5 Programming是迄今为止我见过的新手前端开发人员最好的书。 这是学习HTML5的最佳书籍列表: HTML5:启动并运行 首先是HTML5编程 适用于Mastermi ...
  • 是的,这可能是预期的行为(取决于你正在做的事情),对于IE8来说是完全正常的。 在IE8中,很多HTML5标记/元素都不会被浏览器理解,并且它不知道它是否应该在线显示或作为块显示,并且您需要提供HTML5垫片来处理这些内容。 IE8中支持的CSS选择器规则也很少,在Selectivizr中可以找到如何获得支持的列表。 当然,您需要像JQuery这样的东西来提供支持 还有一些:伪属性不受支持,你需要编写JavaScript来模拟它们并添加适当的样式规则。 IE9以下的IE无法呈现HTML5 / CSS3布局还 ...
  • 您尝试在WebView加载/播放的游戏不是纯HTML5应用程序,而是简单的JavaScript游戏。 要在您自己的WebView ,您需要为此WebView激活JavaScript : WebView myWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); 在我的测试用例中,这也不起 ...
  • 那么,既然你提到了CSS3,这里有一些CSS3淡入淡出的解决方案。 HTML
    You need a username that is more than 5 letters long
    CSS .msg { color:#999; opacity:0; display:inline; ...
  • 简而言之: 优点: 支持互动媒体 改进了语义 更直观的开发 缺点: 仅在现代浏览器上支持 首先,要记住语言本身不被认为是完成的关键。 因此,HTML5是早期版本的延续,被认为是对语言的改进。 正如您在问题中提到的那样,HTML5的主要缺点是它仅受现代浏览器的支持(请参阅http://html5test.com/ )。 因此,如果您的目标受众无法访问现代浏览器,HTML5肯定会处于劣势。 这也是使用CSS3的主要缺点。 优势肯定超过利弊。 这是一个很好的资源,概述了使用HTML5的许多差异和优势 - http ...
  • 你总是希望在这里进行测试。 但您也可以定期运行它们。 http://validator.w3.org/ You always want to run tests on here towards the end. But you can also run them periodically. http://validator.w3.org/

相关文章

更多

最新问答

更多
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • Java中的不可变类(Immutable class in Java)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • EXCEL VBA 基础教程下载
  • RoR - 邮件中的动态主体(部分)(RoR - Dynamic body (part) in mailer)
  • 无法在Google Script中返回2D数组?(Can not return 2D Array in Google Script?)
  • JAVA环境变量的设置和对path , classpth ,java_home设置作用和目的?
  • mysql 关于分组查询、时间条件查询
  • 如何使用PowerShell匹配运算符(How to use the PowerShell match operator)
  • Effective C ++,第三版:重载const函数(Effective C++, Third edition: Overloading const function)
  • 如何用DELPHI动态建立MYSQL的数据库和表? 请示出源代码。谢谢!
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 使用前端框架带来哪些好处,相对于使用jquery
  • Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)
  • 高考完可以去做些什么?注意什么?
  • 如何声明放在main之后的类模板?(How do I declare a class template that is placed after the main?)
  • 如何使用XSLT基于兄弟姐妹对元素进行分组(How to group elements based on their siblings using XSLT)
  • 在wordpress中的所有页面的标志(Logo in all pages in wordpress)
  • R:使用rollapply对列组进行求和的问题(R: Problems using rollapply to sum groups of columns)
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • python中使用sys模块中sys.exit()好像不能退出?
  • 将Int拆分为3个字节并返回C语言(Splitting an Int to 3 bytes and back in C)
  • 在SD / MMC中启用DDR会导致问题吗?(Enabling DDR in SD/MMC causes problems? CMD 11 gives a response but the voltage switch wont complete)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 如何将字符串转换为Elixir中的函数(how to convert a string to a function in Elixir)