Making different threads use different stdout's

This is a discussion on Making different threads use different stdout's within the C++ Programming forums, part of the General Programming Boards category; Sup all, I'm using cout a lot in my program in multiple threads. I'm crashing in one of the threads, ...

  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
    5,871
    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%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 12: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, 12: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, 04:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21