同步STD cout输出多线程 [英] Synchronizing STD cout output multi-thread

查看:269
本文介绍了同步STD cout输出多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Latelly我一直在使用多线程编码,经过一段时间的写作,我意识到如果我在不同的boost ::线程中使用std :: cout,输出将没有一个逻辑顺序,程序, m测试是这样的:

Latelly I've been working with multi-thread coding, after a while writing I realized that if I used std::cout in different boost::threads, the output would came without a logical order, the program that I'm testing is something like:

#include <boost/thread/thread.hpp>
#include <iostream>

int my01( void )
{
    std::cout << "my01" << std::endl;
    return 0;
}
/* my02, my03 and my04 are the same with different outputs*/
[...]
int main( void )
{
    boost::thread t1(&my01);
    boost::thread t2(&my02);
    boost::thread t3(&my03);
    boost::thread t4(&my04);

    while(!t1.joinable() || !t2.joinable() || !t3.joinable() || !t4.joinable());

    t1.join();
    t2.join();
    t3.join();
    t4.join();

    std::cout << "The end!" << std::endl;
    getchar();
    return 0;
}



输出通常是):


And the output is usually like (it changes):


my02my01

my04

my03

BLANK LINE

结束!

my02my01
my04
my03
BLANK LINE
The end!

考虑到这个问题,我想创建一个单线程管理所有输出,因此它们的顺序如下:

With this issue in mind I was thinking of creating a single thread to manage all of the outputs, so they would be in order like:


my01

my02

my03

my04

结束!

my01
my02
my03
my04
The end!

哪个是写这样的线程或管理这些输出的最佳方式?

请阅读此问题的答案:是cout同步/线程安全吗?


Ps:我使用Visual C ++ 2010 Express和我的cpu有8个不同的内核。 br>

谢谢您的时间!

Which is the optimal way to write such thread or to manage those outputs?
Please read the answers to this question too: Is cout synchronized/thread-safe?

Ps:I'm using Visual C++ 2010 Express and my cpu has 8 different cores.

Thank you for your time!

推荐答案

首先,避免所有显式线程管理,而是使用 std :: async 以某些任意数量的单独线程启动任务。

First of all, you might consider avoiding all the explicit thread management, and instead use std::async to launch your tasks in some arbitrary number of separate threads.

第二,不是在线程本身中执行I / O,而是要创建结果,并连续地执行输出。这意味着线程函数只是创建一些数据,并留给调用者来写出来:

Second, instead of doing the I/O in the threads themselves, you want to create results, and do the output itself serially. This means the thread function just creates some data, and leaves it to the caller to actually write that out:

// warning: untested code
std::string process(int value) {
     std::ostringstream buffer;
     buffer << "my" << std::setfill('0') << std::setw(2) << value;
     return buffer.str();
}

然后我们需要启动异步调用四个副本:

Then we need to launch invoke four copies of that asychronously:

std::vector<std::future<std::string> > results;

for (int i=0; i<4; i++)
    results.push_back(std::async(process, i));

然后我们等待结果并按顺序打印出来:

Then we wait for the results and print them out in order:

for (int i=0; i<4; i++)
    std::cout << results[i].get();

我应该添加一个小点:我基于标准的C ++ 11 future s。我相信基本的想法也应该使用Boost future s(标准是基于的),但还没有测试它。对Boost的期货进行一些小的调整(例如,命名) 。(虽然如上所述,代码未经测试,但是标准期货可能需要进行一些调整,虽然希望减少)。

I should add one minor point: I'm basing this on standard C++11 futures. I believe the basic idea should also work with Boost futures (upon which the standard was based) but haven't tested it. Some minor adjustments (e.g., to names) will be needed to work with Boost's futures (though since, as noted, the code isn't tested, a few adjustments may be needed with standard futures too, though hopefully fewer anyway).

这篇关于同步STD cout输出多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆