1

I've looked everywhere for an example and checked the C++ manual (I learn best by example).

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. Originally I've been using's C's write but I lose a few points for doing this.

Other solutions I thought of was using a semaphore to protect cout, so it can only print for one thread at a time. But I get the feeling that there's a built in one for C++ somewhere out there...

Help will be greatly appreciated. And please don't link me to anything from http://www.cplusplus.com/ without giving me an example. 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.

Edit: More info pertaining to my question. No C++11 is not allowed. 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.

Final Edit: itwasntpete was the closest to the correct answer, but I can't choose comments. 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. But we're not allowed to use other libraries. It was easier to make that as a rule for people trying to help. Sorry!

Tim Z.
  • 251
  • 1
  • 12
  • IIRC it's OS and implementation dependent ... – πάντα ῥεῖ Dec 05 '13 at 20:22
  • Is `c++11` available? – Chad Dec 05 '13 at 20:23
  • What do you mean by blocking exactly? Preventing threads from writing over each other? – Zan Lynx Dec 05 '13 at 20:26
  • Yes, because of context switches, threads may interrupt the stream can print in the middle of the current string. Like if I had two strings, "cat" and "dog" and two threads that printed those two. I could get a result of "cadogt" and etc. – Tim Z. Dec 05 '13 at 20:37
  • 1
    there is no built in one, you have to do it by yourself. since you know about semaphore, you also should know about [mutex](http://www.cplusplus.com/reference/mutex/mutex/?kw=mutex). – user1810087 Dec 05 '13 at 20:39
  • @itwasntpete My prof, while being a genius, isn't the most clear at times. The dilemma I have with that is, I'm not allowed globals. But I have multiple threads and different files that will need to see said semaphore. Which.. puts me into one helluva bind... Unless... oh... I could always pass a function pointer around.... This sounds horrible.. Anyone have a better suggestion? – Tim Z. Dec 05 '13 at 20:43
  • it's not about globals, [shared_ptr](http://www.cplusplus.com/reference/memory/shared_ptr/), [singleton](http://en.wikipedia.org/wiki/Singleton_pattern) could help you. – user1810087 Dec 05 '13 at 20:50
  • 1
    Multithreading is impossible in C++ before C++11 without "third party libraries." Look at what "third party library" you are using to achieve concurrency, and use its mutual exclusion mechanism to provide exclusive access to `cout`. – Casey Dec 05 '13 at 20:58

3 Answers3

2

I don't think there's any synchronization built in for streams. In C++03 cout is even not necessarily thread-safe. In c++11 it is but still not synchronized.

See this question: Is cout synchronized/thread-safe?

Community
  • 1
  • 1
Jan Korous
  • 666
  • 4
  • 11
0

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.

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

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

  • that's not realy an answer. OP asking about preventing different thread's of interfere each other, not thread's. also [boost 1.38.0](http://www.boost.org/users/history/version_1_38_0.html) is from feb. 2009. – user1810087 Dec 05 '13 at 20:35
  • "write to standard out with blocking for a concurrent assignment" ? wouldn't threads work? –  Dec 05 '13 at 20:45
  • somehow thread's are involved, but the question is about syncing the concurrency. – user1810087 Dec 05 '13 at 20:52
0

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

You'll have to adapt it to use it with your threading library instead of 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;
}
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • @TimZ. Yes I know. I added an edit to say you'd need to adapt it to your threading library. I can't write sample code with libraries I don't know about so I used Boost. – Zan Lynx Dec 05 '13 at 21:40