weird output when I use pthread and printf

后端 未结 5 1068
执念已碎
执念已碎 2020-12-19 11:57

I write a program using pthread.

Environment:windows 7 , CYGWIN_NT-6.1 i686 Cygwin , gcc (GCC) 4.5.3

The source code

#include<         


        
5条回答
  •  情话喂你
    2020-12-19 12:28

    This looks like it may be a bug in Cygwin, or maybe something is misconfigured. Several answer here indicate that 'thread safe' only promises that the function won't cause harm to the program, and that thread safety doesn't necessarily mean that a function is 'atomic'. But, as far as I know, POSIX doesn't formally define 'thread safe' (if anyone has a pointer to such a definition, please post it in a comment).

    However, not only does POSIX specify that printf() is thread safe, POSIX also specifies that:

    All functions that reference ( FILE *) objects shall behave as if they use flockfile() and funlockfile() internally to obtain ownership of these ( FILE *) objects.

    Since printf() implicitly references the stdout FILE* object, all printf() calls should be atomic with respect to each other (and any other function that uses stdout).

    Note that this might not be true on other systems, but in my experience it does hold true for many multi threaded systems.

提交回复
热议问题