std::cout 插入运算符的线程安全

Pau*_*oyd 5 multithreading valgrind c++11 thread-sanitizer

我一直认为usingstd::cout << something是线程安全的。

对于这个小例子

#include <iostream>
#include <thread>

void f()
{
   std::cout << "Hello from f\n";
}

void g()
{
   std::cout << "Hello from g\n";
}

int main()
{
   std::thread t1(f);
   std::thread t2(g);
   t1.join();
   t2.join();
}
Run Code Online (Sandbox Code Playgroud)

我的期望是两个输出的顺序是未定义的(实际上这就是我在实践中观察到的),但调用是operator<<线程安全的。

然而,ThreadSanitizer、DRD 和 Helgrind 似乎都给出了有关访问 std::__1::ios_base::width(long) 和 std::__1::basic_ios<char, std::__1::char_traits >:: 的各种错误充满()

在编译器资源管理器上我没有看到任何错误

在 FreeBSD 13 上,ThreadSanitizer 给了我 3 个警告,上面列出的两个警告加上底层 I/O 缓冲区的 malloc/memcpy。

同样在 FreeBSD 13 中,DRD 给出 4 个错误,width()两个fill()线程乘以 2 个错误。

最后,FreeBSD 13 Helgrind 在线程创建中给出了一个与 TLS 相关的已知误报,fill()并且width()两次。

在 Fedora 34 上

  • g++ 11.2.1 和 ThreadSanitizer 没有错误
  • DRD 抱怨 fwrite 中的 malloc/memcpy 使用 g++ 编译的 exe
  • Helgrind 还抱怨 fwrite 以及 的构建cout,再次使用 g++ 编译的 exe
  • clang++ 12 ThreadSanitizer 抱怨fill()width()
  • 使用 clang++ 编译器 exe 的 DRD 抱怨fill(), width(),fwrite以及其中的另一个start_thread
  • Helgrind 与 clang++ exe 抱怨一些TLS fill()、、、width()fwrite

macOS XCode clang++ ThreadSanitizer 也会生成警告(将是 libc++)。

查看 libc++ 和 libstdc++ 代码,我没有看到任何保护width(). 所以我不明白为什么编译器资源管理器没有投诉。

我尝试使用 TSAN_OPTIONS=print_suppressions=1 运行,但没有更多输出(g++ Fedora ThreadSanitizer)

width()对于和呼吁似乎确实达成了一些共识fill()

更仔细地查看 libstdc++ 源代码,我发现有(经过一些修剪和注释):

// ostream_insert.h
// __n is the length of the string pointed to by __s
  template<typename _CharT, typename _Traits>
    basic_ostream<_CharT, _Traits>&
    __ostream_insert(basic_ostream<_CharT, _Traits>& __out,
             const _CharT* __s, streamsize __n)
{
    typedef basic_ostream<_CharT, _Traits>       __ostream_type;
    typedef typename __ostream_type::ios_base    __ios_base;

    typename __ostream_type::sentry __cerb(__out);
    if (__cerb)
    {
        __try
        {
            const streamsize __w = __out.width();
            if (__w > __n)
            {
                // snipped
                // handle padding
            }
            else
              __ostream_write(__out, __s, __n);
          // why no hazard here?
          __out.width(0);
      }

Run Code Online (Sandbox Code Playgroud)

__out是流对象,cout在本例中是全局的。我没有看到任何像锁或原子之类的东西。

关于 ThreadSanitizer/g++ 如何获得“干净”的输出有什么建议吗?

有这样一个有点神秘的评论


  template<typename _CharT, typename _Traits>
    basic_ostream<_CharT, _Traits>::sentry::
    sentry(basic_ostream<_CharT, _Traits>& __os)
    : _M_ok(false), _M_os(__os)
    {
      // XXX MT
      if (__os.tie() && __os.good())
    __os.tie()->flush();
Run Code Online (Sandbox Code Playgroud)

libc++ 代码看起来类似。在iostream

template<class _CharT, class _Traits>
basic_ostream<_CharT, _Traits>&
__put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
                          const _CharT* __str, size_t __len)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
    try
    {
#endif // _LIBCPP_NO_EXCEPTIONS
        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
        if (__s)
        {
            typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
            if (__pad_and_output(_Ip(__os),
                                 __str,
                                 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
                                     __str + __len :
                                     __str,
                                 __str + __len,
                                 __os,
                                 __os.fill()).failed())
                __os.setstate(ios_base::badbit | ios_base::failbit);
Run Code Online (Sandbox Code Playgroud)

并在locale


template <class _CharT, class _OutputIterator>
_LIBCPP_HIDDEN
_OutputIterator
__pad_and_output(_OutputIterator __s,
                 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
                 ios_base& __iob, _CharT __fl)
{
    streamsize __sz = __oe - __ob;
    streamsize __ns = __iob.width();
    if (__ns > __sz)
        __ns -= __sz;
    else
        __ns = 0;
    for (;__ob < __op; ++__ob, ++__s)
        *__s = *__ob;
    for (; __ns; --__ns, ++__s)
        *__s = __fl;
    for (; __ob < __oe; ++__ob, ++__s)
        *__s = *__ob;
    __iob.width(0);
    return __s;
}
Run Code Online (Sandbox Code Playgroud)

我再次看到没有线程保护,但这次工具也检测到了危险。

这些是真正的问题吗?对于普通调用,operator<<的值width不会改变,并且始终为 0。

Pau*_*oyd 0

我从乔纳森·韦克利那里得到了答案。让我觉得自己相当愚蠢。

不同之处在于,在 Fedora 上,libstdc++.so 包含 iostream 类的显式实例化。libstdc++.so 未针对 ThreadSanitizer 进行检测,因此它无法检测与其相关的任何危险。