locked
Is the function printf thread safe? RRS feed

  • Question

  • Hi,

    Is the function "printf" thread safe? Visual studio 2005/2003 C++ compiler. We are seeing random crashes in a multithreaded environment mostly in printf and are trying to investigate in this.

    - Kedar

    Tuesday, August 8, 2006 6:05 AM

Answers

  • The method itself operates just fine in multithreaded software. It does not, however, take any responsibility for the parameters that are being passed to it. If thread B happens to delete a char pointer which thread A is about to pass along to printf, you may find yourself crashing. Printf will blindly do what you tell it to do. The concurrency measurements are left up to you.
    Tuesday, August 8, 2006 7:16 AM
  •  

    The problem for the crasher was our linking to non-multithreaded version of libraries. Our build script should have set USE_LIBCMT flag but was missing this for some reason. You can find more about this on MSDN

    Sorry about being lazy and updating this immediately. Hope this provides help to someone whose code crashes in printf for no reason ;)

    Thanks einaros.

     

    - Kedar

    Wednesday, August 23, 2006 9:45 PM

All replies

  • The method itself operates just fine in multithreaded software. It does not, however, take any responsibility for the parameters that are being passed to it. If thread B happens to delete a char pointer which thread A is about to pass along to printf, you may find yourself crashing. Printf will blindly do what you tell it to do. The concurrency measurements are left up to you.
    Tuesday, August 8, 2006 7:16 AM
  • Thanks einaros.

    Why does MSDN not mention the fact that printf is thread safe? Any ideas

    Thanks,

    Kedar

    Tuesday, August 8, 2006 5:35 PM
  • Printf is part of the C run time libraries, which used to come in both multi threaded and single threaded flavor. At some point, the latter was removed, so basically all methods of the CRT are now thread safe no matter what.
    Tuesday, August 8, 2006 5:43 PM
  •  

    The problem for the crasher was our linking to non-multithreaded version of libraries. Our build script should have set USE_LIBCMT flag but was missing this for some reason. You can find more about this on MSDN

    Sorry about being lazy and updating this immediately. Hope this provides help to someone whose code crashes in printf for no reason ;)

    Thanks einaros.

     

    - Kedar

    Wednesday, August 23, 2006 9:45 PM
  •  Kedar Borhade wrote:

     The problem for the crasher was our linking to non-multithreaded version of libraries. Our build script should have set USE_LIBCMT flag but was missing this for some reason. You can find more about this on MSDN

    Sorry about being lazy and updating this immediately. Hope this provides help to someone whose code crashes in printf for no reason ;)

     Thanks to everybody who helped figure this out  

    I haven't had this specific issue, but it helped shed some light on a general question I had.  Once again, thanks!

    Thursday, August 24, 2006 2:42 AM