简体   繁体   English

如何使用阻塞向cout写入?

[英]How can I write to cout with blocking?

I've looked everywhere for an example and checked the C++ manual (I learn best by example). 我到处都在寻找示例,并查看了C ++手册(通过示例,我学得最好)。

What I need is a method that can write to standard out with blocking for a concurrent assignment. 我需要的是一种可以通过阻塞并发分配来写出标准的方法。

I was suggested to use "protected cout" but I have no idea what is meant by that. 建议我使用“受保护的cout”,但我不知道这是什么意思。 Originally I've been using's C's write but I lose a few points for doing this. 最初,我一直在使用C的写操作,但是这样做会损失一些点。

Other solutions I thought of was using a semaphore to protect cout, so it can only print for one thread at a time. 我想到的其他解决方案是使用信号量来保护cout,因此它一次只能打印一个线程。 But I get the feeling that there's a built in one for C++ somewhere out there... 但是我感觉到那里有C ++内置的...

Help will be greatly appreciated. 帮助将不胜感激。 And please don't link me to anything from http://www.cplusplus.com/ without giving me an example. 而且,请不要在没有给出示例的情况下将我链接到http://www.cplusplus.com/上的任何内容。 I'm rather new to C++ and if I was a pro at reading the api at cplusplus.com, I wouldn't be asking this question. 我是C ++的新手,如果我是cplusplus.com上的api专业人员,我不会问这个问题。

Edit: More info pertaining to my question. 编辑:更多信息有关我的问题。 No C++11 is not allowed. 不允许使用C ++ 11。 I am not allowed any 3rd party libraries. 我不允许任何第三方图书馆。 So boost is a no go. 所以提振是不行的。 The machine this has to perform on is a Unix machine. 必须在其上执行的机器是Unix机器。

Final Edit: itwasntpete was the closest to the correct answer, but I can't choose comments. 最终编辑:itwasntpete最接近正确答案,但是我不能选择评论。 Semaphores is the way I have to go. 信号量是我必须走的路。 @Casey true, I'm using a 3rd party library the prof wrote that simplifies concurrency for us. @Casey是的,我正在使用教授编写的第3方库,该库为我们简化了并发。 But we're not allowed to use other libraries. 但是我们不允许使用其他库。 It was easier to make that as a rule for people trying to help. 通常,尝试帮助的人更容易做到这一点。 Sorry! 抱歉!

I don't think there's any synchronization built in for streams. 我认为流没有内置任何同步。 In C++03 cout is even not necessarily thread-safe. 在C ++ 03中,cout甚至不一定是线程安全的。 In c++11 it is but still not synchronized. 在c ++ 11中,它是但仍未同步。

See this question: Is cout synchronized/thread-safe? 看到这个问题: cout是同步的/线程安全的吗?

C++11 has support for threads, otherwise you can use OS dependent threading or an easier route might be libraries such as boost which has support for threads, and in a uniform way. C ++ 11支持线程,否则您可以使用与操作系统有关的线程,或者更简单的方法可能是诸如boost的库,它以统一的方式支持线程。

Boost: http://www.boost.org/doc/libs/1_38_0/doc/html/thread.html 提升: http//www.boost.org/doc/libs/1_38_0/doc/html/thread.html

C++11: http://en.cppreference.com/w/cpp/thread C ++ 11: http//en.cppreference.com/w/cpp/thread

Here is some code that should do what you want. 这是一些应该执行您想要的代码。 You need to link it with boost_thread-mt and pthread, perhaps like gcc -pthread test.cpp -o test -lboost_thread-mt 您需要将其与boost_thread-mt和pthread链接,例如gcc -pthread test.cpp -o test -lboost_thread-mt

You'll have to adapt it to use it with your threading library instead of Boost. 您必须对其进行调整以使其与线程库一起使用,而不是与Boost一起使用。

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

class Debug {
    private:
    static boost::mutex mutex;
    std::ostream &os;

    public:
    Debug(std::ostream &os) : os(os)
    {
        mutex.lock();
    }
    ~Debug()
    {
        mutex.unlock();
    }

    template<typename T> friend const Debug& operator<<(const Debug &d, const T& x)
    {
        d.os << x;
        return d;
    }

    friend const Debug& operator<<(const Debug &d, std::ostream& (*x)(std::ostream&))
    {
        d.os << x;
        return d;
    }
};

boost::mutex Debug::mutex;

using namespace std;
using boost::thread;

void f(int i)
{
    Debug(cout) << "This is " << i << " a test" << endl;
}

int main()
{
    thread t1(f, 1);
    thread t2(f, 2);
    thread t3(f, 3);

    t1.join();
    t2.join();
    t3.join();
    return 0;
}

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

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