简体   繁体   English

在boost :: thread中同步STD cout输出,而无需控制线程

[英]Synchronizing STD cout output in a boost::thread without control over threads

I want to ask exactly the same question as was asked here: Synchronizing STD cout output multi-thread with the single exception that i DO NOT have control over the code in which boost::thread s are used (that code is internal to this library , and I should not / want not to touch it, unless this problem can not be solved in any other way, in which case I could add it to my fork of the library.) 我想问的问题与此处完全相同: 同步STD cout输出多线程 ,但有一个例外,即我无法控制使用boost :: thread的代码(该代码在该库的内部) ,而且我不应该/不想碰它,除非无法以其他任何方式解决此问题,在这种情况下,我可以将其添加到库的fork中。)

The library at a certain time spawns 4 threads (I'm on a mid-2010 MacBook Pro using OSX 10.10) in the C++ code that is executed in a single thread I use cout for logging, like so: 该库在特定时间在C ++代码中生成4个线程(我使用OSX 10.10于2010年中的MacBook Pro),该线程在一个使用cout进行记录的单线程中执行,如下所示:

cout << "Writing file: " << "\\n" << xmlFileName << endl;

This output gets garbled due to multi-threading so it gets hard to understand what is going on in a single thread at any given moment... 由于多线程,该输出出现乱码,因此很难理解在任何给定时刻单线程中正在发生什么...

How can I get the output synchronized ? 如何使输出同步? The project uses Boost 1.57.0 so I can use that. 该项目使用Boost 1.57.0,所以我可以使用它。 I have never had to deal with multi-threading before but I understand the concepts and problems that arise. 我以前从来没有处理过多线程,但是我了解出现的概念和问题。 I have also seen some examples on SO but all of them require access to the code that spawns threads, either using a shared Mutex passed to the threads or using a lockGuard . 我还看到了一些关于SO的示例,但所有示例都需要使用传递给线程的共享Mutex或使用lockGuard来访问生成线程的代码。

Although I have the feeling I can not escape modifying the library code, I hope that this is somehow possible? 尽管我有种无法摆脱的修改库代码的感觉,但我希望这有可能吗?

  1. You could get extremely hacky and write a sycnhronizing stream buffer. 您可能会变得非常骇客,并编写一个同步流缓冲区。

    You could then use that on the cout object: 然后可以在cout对象上使用它:

     std::cout.rdbuf(new my_sync_cout_buf()); 

    before you start the threads. 在启动线程之前。

    Note that this will require some amount of fidgeting to respond to flushes in a nice fashion. 请注意,这将需要一定的坐立不安才能很好地响应冲洗。 Doing this will break down if (one of the) threads uses std::unitbuf or std::flush liberally. 如果(其中一个)线程自由地使用std::unitbufstd::flush ,则此操作将失败。

    In the absolute worst case you'd have to switch on thread id and interlace the output (eg line-by-line) 在绝对最坏的情况下,您必须打开线程ID并交织输出(例如逐行)

    THINKO: the ostream object itself would still be non-threadsafe. THINKO: ostream对象本身仍然是非线程安全的。 So it's just option #2 then, really: 因此,实际上,这只是选项#2:

  2. Perhaps it's easier (and saner) to just open pipes from one thread and read them from another. 从一个线程打开管道并从另一个线程读取它们也许更容易(也更精明)。 This way you can coordinate the order in which output is done yourself. 这样,您可以协调自己完成输出的顺序。 Of course, since you don't control the threads, you'd have to fork instead of starting threads. 当然,由于您不控制线程,因此必须分叉而不是启动线程。

    Then, in each child process, you can setup the stream buffer of std::cout to write to the pipe 然后,在每个子进程中,您可以设置std::cout的流缓冲区以写入管道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM