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 在所示位置显示了两个线程的崩溃。堆栈如下:

堆栈 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但我没有任何运气。

实际上,生产者将A它们的许多不同子类存储type_id在一个std::map<type_id const*,std::shared_ptr<A>>(每种类型一个实例)中,我按类型从消费者那里查找。

我的理解是std::shared_ptr这些类型的操作是线程安全的。我错过了什么?

4

1 回答 1

3

shared_ptr在其控制块上具有线程安全性。当 ashared_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 中编写的。此时,gcc 尚未完全实现原子shared_ptr访问,如第20.7.2.5节所述

于 2013-05-17T21:07:15.243 回答