c ++多进程写入同一个文件 - 进程间互斥?(c++ multiple processes writing to the same file - Interprocess mutex?)
我的问题是:从多个进程写入文件的最佳方式(或至少是一种有效的方法)是什么?
注意:我正在使用c ++ 11,我希望这可以在任何平台上运行(即纯c ++代码)。
我已经做了一些研究,下面是我所得出的结论:
- 在我的进程中,我有多个线程。 这很容易在每个进程中使用互斥来处理对文件的访问。
- 一个c ++ / c ++ 11互斥体或条件变量不能用于序列化进程之间。
- 我需要某种外部信号量/锁定文件来充当“互斥体”......但我不确定如何去做这件事。
我看到应用程序在使用时会使用诸如创建“.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:
- In my processes I have multiple threads. This is easily handled within each process using a mutex to serialise access to the file.
- A c++/c++11 mutex or conditional variable cannot be used to serialise between processes.
- 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
满意答案
你可以像下面的代码那样做。 需要进行一些调整以获得您想要的确切结果(如图像),但我相信它会让您顺利进行。
我为'进步'栏添加了一些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%"
tostyle="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>
相关问答
更多HTML5 / CSS3无类框架(HTML5/CSS3 classless framework)
如何关注HTML5和CSS3中的最新更新[关闭](How to follow the latest updates in HTML5 and CSS3 [closed])
HTML5 / CSS3布局显示任务运行状态[关闭](HTML5/CSS3 layout to show tasks running status [closed])
我如何知道何时使用CSS,ID或HTML5来使用CSS在HTML中进行布局?(How do I know when to use a Class, an ID or HTML5 to make layouts in HTML using CSS? [closed])
HTML5,CSS3参考[关闭](HTML5, CSS3 reference [closed])
布局网站IE8和下层看起来错了CSS3 [关闭](Layout website IE8 and lower looks wrong CSS3 [closed])
Android HTML5游戏[关闭](Android HTML5 Games [closed])
HTML5和CSS3在元素焦点上显示表单提示(HTML5 and CSS3 show form hints on element focus)
HTML5 + CSS3 VS标准HTML 4 [关闭](HTML5 + CSS3 VS Standard HTML 4 [closed])
如何检查符合html5 / css3的网页[关闭](how to check for html5/css3 compliant web pages [closed])
相关文章
更多Java 流(Stream)、文件(File)和IO
My W3C Custom Mapping File
C程序访问Hadoop
《虚拟机系统与进程的通用平台》(Virtual Machines: Versatile Platforms for Systems and Processes)扫描版[PDF]
shell 脚本执行,出现错误bad interpreter: No such file or directory
《C/C++图像处理编程》扫描版[PDF]
Android 微信的回调跨进程间的调用 (分享)
C++ Hadoop实战备忘
Python内建函数(C)
java server怎样和c++ client实现SSL通信??
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!