Understanding pthread_detach

前端 未结 1 1177
遇见更好的自我
遇见更好的自我 2021-01-04 07:55

The following prints

In Main()
Hello World
Hello World

Why does this print Hello World twice? If I use pthread_join() the desired output oc

1条回答
  •  生来不讨喜
    2021-01-04 08:28

    In my opinion you are using a non-thread-safe version of the standard library (prints, fflush...). I have already seen this kind of (apparently) non-logical behavior on a old unix-like real time system. There were two different versions of std library, one for single-threaded mode and one for multithreaded. Of course, the default was single threaded... In general, accesses to file pointers and similar things should be serialized with mutexes. In your program there are two thread terminations, each may want to call implicitly an fflush, but since the underlying buffers are not meant to be accessed concurrently, it may happen that both flushes write the same data to the output file descriptor.

    0 讨论(0)
提交回复
热议问题