thread synchronization

This is a discussion on thread synchronization within the C++ Programming forums, part of the General Programming Boards category; Just a quick question... If I have a console app with multiple threads and more than one thread prints status ...

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    thread synchronization

    Just a quick question...

    If I have a console app with multiple threads and more than one thread prints status information in a log style to the console output using printf would I need to enclose any printf statements within a critical section to avoid a mix of two messages being printed to the screen if two threads happened to printf at exactly the same time?

    I haven't had it occur yet as I'm sure it would be quite a rare occurence but I was wondering, is it actually possible for that to happen?

    I guess the same goes for outputing to a log file using the stdio functions.

    tia

    Daniel.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Anytime you have the possibility of a collision like that you should use some form of synchronisation.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I don't think its possible but check your library's documentation. They should be labeled "thread-safe" or "not thread-safe." If they're not labeled at all, best bet on that its not since the developers didn't have threads in mind when making it. Most of the C/C++ library is not-thread safe as they only suggest one thread being executed (UNIX style I guess, one thread per process).

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The language itself is really oblivious to threading because that's something that's heavily os dependant and also would limit c++ to just multithreading os's (unless the implementor of the compiler wants a lot more work and the os allows it ). The only thing that really shows they acknowledge threading is volatile.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Speedy5
    I don't think its possible but check your library's documentation. They should be labeled "thread-safe" or "not thread-safe." If they're not labeled at all, best bet on that its not since the developers didn't have threads in mind when making it. Most of the C/C++ library is not-thread safe as they only suggest one thread being executed (UNIX style I guess, one thread per process).
    Thread safety is not the issue here...that usually relates to how the library has been implemented (ie using static buffers for multiple threads or relating a buffer to a specific thread)......

    The problem is that there's no guarantee that any function can complete without being preempted unless you use one of the OS's mechanisms to control syncronisation.......so 2 calls (1 from each thread) printing "Hello World" might actually print "HellHello WorldoWorld"......this isnt allways the case, but it certianly is possible.....best bet, wherever 2 threads compete for a resource (even screen access) use a form of syncronisation

  6. #6
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    thanks for the really helpful replys people. Much appreciated, I don't know where I'd be without help from people like you.

    Later

    Dan
    Last edited by codec; 03-12-2003 at 08:17 AM.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Actually, Fordy, what we mean by thread safety is this:

    1) Not using global buffers (static)
    2) Locking the device/stream being used so that the ouput/input is uninterrupted. That way you can avoid "HellHello World World". In Java, this can be achieved to some degree using the syncronized keyword. .NET also has a similar feature.
    3) etc

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I think thread-safe is a design and implementation issue and should not rely on programming language or OS. Yes, an Windows and Linux provide a foundation for multithreads and multiprocesses. However, you are responsible for data synchronization.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. thread synchronization
    By radeberger in forum C++ Programming
    Replies: 13
    Last Post: 03-29-2009, 10:21 AM
  2. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 04:34 AM
  3. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 10:34 PM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. Thread Synchronization :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-09-2002, 09:09 AM

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