Thread: Making different threads use different stdout's

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    73

    Making different threads use different stdout's

    Sup all, I'm using cout a lot in my program in multiple threads. I'm crashing in one of the threads, but I haven't a clue where. My job would be a lot easier if the threads' cout's weren't intermingled.

    Is there a way to have my two different threads use two different cout's? Or is there another way to distinguish one threads output from another's?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is only ever one cout. If multiple threads need to write to it, then they need to explicitly synchronise. For example, by grabbing a critical section or mutex object, then writing to cout, then releasing the synchronisation object afterwards.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm crashing in one of the threads, but I haven't a clue where.
    This is what debuggers are for.
    Debugging by print statements only works for simple student homework. For more advanced programs (and problems), you need the tools!


    If you've got two threads sending stuff to cout in a chaotic manner, you might consider creating a third thread which is just for output. T1 and T2 send messages to T3 when they want to output something.

    T3 can then buffer messages from T1 and T2, and display
    T1
    T2
    T1
    T2
    or
    T1
    T1
    T1
    T2
    T2
    T2
    or you can start to implement simple controls for T3, like saying "output T1 to file".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdout with JNI
    By GoLuM83 in forum C Programming
    Replies: 4
    Last Post: 12-14-2006, 01:27 PM
  2. a point of threads to create multiple threads
    By v3dant in forum C Programming
    Replies: 3
    Last Post: 10-06-2004, 09:48 AM
  3. Replies: 2
    Last Post: 01-13-2003, 01:28 PM
  4. People Making New Threads
    By Okiesmokie in forum Game Programming
    Replies: 3
    Last Post: 06-23-2002, 01:18 AM
  5. stdout
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-25-2002, 05:43 AM