2

std::shared_ptrあるスレッドによって生成され、別のスレッドによって消費されるデータを指すために使用しようとしています。ストレージ フィールドは、基本クラスへの共有ポインターです。

問題を再現した、私が作成できる最も簡単な Google テストを次に示します。

#include "gtest/gtest.h"
#include <thread>

struct A 
{
  virtual ~A() {}
  virtual bool isSub() { return false; }
};

struct B : public A
{
  bool isSub() override { return true; }
};

TEST (SharedPointerTests, threadedProducerConsumer)
{
  int loopCount = 10000;

  shared_ptr<A> ptr;

  thread producer([loopCount,&ptr]()
  {
    for (int i = 0; i < loopCount; i++)
      ptr = make_shared<B>();              // <--- THREAD
  });

  thread consumer([loopCount,&ptr]()
  {
    for (int i = 0; i < loopCount; i++)
      shared_ptr<A> state = ptr;           // <--- THREAD
  });

  producer.join();
  consumer.join();
}

実行すると、次のようになることがあります。

[ RUN      ] SharedPointerTests.threadedProducerConsumer
pure virtual method called
terminate called without an active exception
Aborted (core dumped)

GDB は、示されている場所に 2 つのスレッドがあるクラッシュを示しています。スタックは次のとおりです。

スタック 1

#0  0x00000000006f430a in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x7fffe00008c0)
    at /usr/include/c++/4.8/bits/shared_ptr_base.h:144
#1  0x00000000006f26a7 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x7fffdf960bc8, 
    __in_chrg=<optimized out>) at /usr/include/c++/4.8/bits/shared_ptr_base.h:553
#2  0x00000000006f1692 in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x7fffdf960bc0, 
    __in_chrg=<optimized out>) at /usr/include/c++/4.8/bits/shared_ptr_base.h:810
#3  0x00000000006f16ca in std::shared_ptr<A>::~shared_ptr (this=0x7fffdf960bc0, __in_chrg=<optimized out>)
    at /usr/include/c++/4.8/bits/shared_ptr.h:93
#4  0x00000000006e7288 in SharedPointerTests_threadedProducerConsumer_Test::__lambda2::operator() (__closure=0xb9c940)
    at /home/drew/dev/SharedPointerTests.hh:54
#5  0x00000000006f01ce in std::_Bind_simple<SharedPointerTests_threadedProducerConsumer_Test::TestBody()::__lambda2()>::_M_invoke<>(std::_Index_tuple<>) (this=0xb9c940) at /usr/include/c++/4.8/functional:1732
#6  0x00000000006efe13 in std::_Bind_simple<SharedPointerTests_threadedProducerConsumer_Test::TestBody()::__lambda2()>::operator()(void) (
    this=0xb9c940) at /usr/include/c++/4.8/functional:1720
#7  0x00000000006efb7c in std::thread::_Impl<std::_Bind_simple<SharedPointerTests_threadedProducerConsumer_Test::TestBody()::__lambda2()> >::_M_run(void) (this=0xb9c928) at /usr/include/c++/4.8/thread:115
#8  0x00007ffff6d19ac0 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007ffff717bf8e in start_thread (arg=0x7fffdf961700) at pthread_create.c:311
#10 0x00007ffff647ee1d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:113

スタック 2

#0  0x0000000000700573 in std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<B, std::allocator<B>, (__gnu_cxx::_Lock_policy)2> > >::_S_destroy<std::_Sp_counted_ptr_inplace<B, std::allocator<B>, (__gnu_cxx::_Lock_policy)2> > (__a=..., __p=0x7fffe00008f0)
    at /usr/include/c++/4.8/bits/alloc_traits.h:281
#1  0x00000000007003b6 in std::allocator_traits<std::allocator<std::_Sp_counted_ptr_inplace<B, std::allocator<B>, (__gnu_cxx::_Lock_policy)2> > >::destroy<std::_Sp_counted_ptr_inplace<B, std::allocator<B>, (__gnu_cxx::_Lock_policy)2> > (__a=..., __p=0x7fffe00008f0)
    at /usr/include/c++/4.8/bits/alloc_traits.h:405
#2  0x00000000006ffe76 in std::_Sp_counted_ptr_inplace<B, std::allocator<B>, (__gnu_cxx::_Lock_policy)2>::_M_destroy (
    this=0x7fffe00008f0) at /usr/include/c++/4.8/bits/shared_ptr_base.h:416
#3  0x00000000006f434c in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x7fffe00008f0)
    at /usr/include/c++/4.8/bits/shared_ptr_base.h:161
#4  0x00000000006f26a7 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x7fffe8161b68, 
    __in_chrg=<optimized out>) at /usr/include/c++/4.8/bits/shared_ptr_base.h:553
#5  0x00000000006f16b0 in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x7fffe8161b60, 
    __in_chrg=<optimized out>) at /usr/include/c++/4.8/bits/shared_ptr_base.h:810
#6  0x00000000006f4c3f in std::__shared_ptr<A, (__gnu_cxx::_Lock_policy)2>::operator=<B>(std::__shared_ptr<B, (__gnu_cxx::_Lock_policy)2>&&) (this=0x7fffffffdcb0, __r=<unknown type in /home/drew/dev/unittests, CU 0x0, DIE 0x58b8c>)
    at /usr/include/c++/4.8/bits/shared_ptr_base.h:897
#7  0x00000000006f2d2a in std::shared_ptr<A>::operator=<B>(std::shared_ptr<B>&&) (this=0x7fffffffdcb0, 
    __r=<unknown type in /home/drew/dev/unittests, CU 0x0, DIE 0x55e1c>)
    at /usr/include/c++/4.8/bits/shared_ptr.h:299
#8  0x00000000006e7232 in SharedPointerTests_threadedProducerConsumer_Test::__lambda1::operator() (__closure=0xb9c7a0)
    at /home/drew/dev/SharedPointerTests.hh:48
#9  0x00000000006f022c in std::_Bind_simple<SharedPointerTests_threadedProducerConsumer_Test::TestBody()::__lambda1()>::_M_invoke<>(std::_Index_tuple<>) (this=0xb9c7a0) at /usr/include/c++/4.8/functional:1732
#10 0x00000000006efe31 in std::_Bind_simple<SharedPointerTests_threadedProducerConsumer_Test::TestBody()::__lambda1()>::operator()(void) (
    this=0xb9c7a0) at /usr/include/c++/4.8/functional:1720
#11 0x00000000006efb9a in std::thread::_Impl<std::_Bind_simple<SharedPointerTests_threadedProducerConsumer_Test::TestBody()::__lambda1()> >::_M_run(void) (this=0xb9c788) at /usr/include/c++/4.8/thread:115
#12 0x00007ffff6d19ac0 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#13 0x00007ffff717bf8e in start_thread (arg=0x7fffe8162700) at pthread_create.c:311
#14 0x00007ffff647ee1d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:113

ここで使用するなど、さまざまなアプローチを試みましstd::dynamic_pointer_castたが、運がありませんでした。

実際には、プロデューサーは、タイプごとにコンシューマーから検索する(タイプごとに 1 つのインスタンス)に、さまざまなサブクラスを格納しAます。type_idstd::map<type_id const*,std::shared_ptr<A>>

私の理解ではstd::shared_ptr、これらのタイプの操作ではスレッドセーフです。私は何が欠けていますか?

4

1 に答える 1

3

shared_ptr制御ブロックにスレッドセーフがあります。がshared_ptr作成され、新しく作成されたリソースを指すと、制御ブロックが作成されます。MSDNによると、これは次のとおりです。

リソースを所有する shared_ptr オブジェクトは、制御ブロックを共有します。制御ブロックは以下を保持します。

  • リソースを所有する shared_ptr オブジェクトの数、
  • リソースを指すweak_ptrオブジェクトの数、
  • そのリソースの削除者がある場合は、その削除者、
  • コントロール ブロックのカスタム アロケータ (ある場合)。

これは、同じメモリを指すshared_ptrの複数のコピーによる同期の問題がないことを保証することを意味します。shared_ptrただし、メモリ自体の同期は管理しません。スレッドセーフに関するセクションを参照してください(強調鉱山)

オブジェクトが所有権を共有するコピーであっても、複数のスレッドが異なるshared_ptr オブジェクトを同時に読み書きできます。

あなたのコードは共有ptrしています。これは、データ競合があることを意味します。また、コンシューマー スレッドの実行がスケジュールされる前にプロデューサー スレッドが複数のオブジェクトを生成する可能性があることにも注意してください。つまり、一部のオブジェクトが失われます。

コメントで指摘されているように、shared_ptr でアトミック操作を使用できます。プロデューサー スレッドは次のようになります。

thread producer([loopCount,&ptr]()
{
    for (int i = 0; i < loopCount; i++)
    {
        auto p = std::make_shared<B>();  // <--- THREAD
        std::atomic_store<A>( &ptr, p );
    }          
});

オブジェクトが作成され、アトミックに に格納されptrます。次に、コンシューマーはオブジェクトをアトミックにロードする必要があります。

thread consumer([loopCount,&ptr]()
{
    for (int i = 0; i < loopCount; i++)
    {
        auto state = std::atomic_load<A>( &ptr );           // <--- THREAD
    }
});

これには、プロデューサー スレッドが複数回の反復実行を許可されている場合、オブジェクトが失われるという欠点があります。

これらの例は、Visual Studio 2012 で作成されました。セクション20.7.2.5shared_ptrで説明されているように、現時点では、gcc はアトミック アクセスを完全には実装していません。

于 2013-05-17T21:07:15.243 に答える