C++11,std::atomic 在 clang 3.2 和 libc++ 中损坏了吗?

标签 c++ c++11 clang atomic libc++

我在使用 XCode 版本 4.6.3 (4H1503) 和 clang 的 Mac OS X 10.8 上遇到了一个奇怪的问题: Apple LLVM 版本 4.2 (clang-425.0.28)(基于 LLVM 3.2svn) 目标:x86_64-apple-darwin12.4.0 线程模型:posix

我创建了一个非常基本的 thread_pool 类并想对其进行压力测试。

    struct last_item_exception {};

    template<class Task>
    struct thread_reclaimer
    {

      explicit
        thread_reclaimer(impl::basic_task_queue<Task>& q)noexcept
          : q_(q)
      {}

      void operator()()
      {
        try
        {
          while(true)
          {
            using namespace boost;
            q_.wait_and_pop()();
          }
        }
        catch(last_item_exception)
        {
          TRACE_LOG << "caught last item exception";
        }
      }

    private:
      impl::basic_task_queue<Task>& q_;
    };



    template<class Task>
    thread_reclaimer<Task> make_thread_reclaimer
      (impl::basic_task_queue<Task>& queue)noexcept
    {
      return std::move(thread_reclaimer<Task>(queue));
    }



  class thread_pool : public boost::noncopyable
  {
    typedef std::function<void()> task_type;

  public:
    explicit thread_pool(size_t number_of_threads)
      : threads_created_()
      , pool_()
      , queue_()
      , done_()
    {
      try
      {
        TRACE_LOG << "thread_pool: creating " <<number_of_threads << " threads";

        while(threads_created_<number_of_threads)
        {
          ++threads_created_;
          pool_.create_thread(make_thread_reclaimer(queue_));
          TRACE_LOG << "thread_pool: created thread number: "
                    << threads_created_ 
          ;
        }
        TRACE_LOG << "thread_pool: all threads started";
      }
      catch(...)
      {
        TRACE_LOG << "thread_pool: exception occured";
        finish_and_join_all();
        throw;
      }
      TRACE_LOG << "thread_pool: constructor finished";
    }

    ~thread_pool()
    {
      finish_and_join_all();
    }

    bool enqueue(task_type t)
    {
      if(done_) return false;
      queue_.push(t, done_);
      return !done_; //if done was set inbetween, no push occured!
    }

    void finish_and_join_all()
    {
      TRACE_LOG << "entered: finish_and_join_all & done is: " << done_;
      if(done_) return;
      done_ = true;

      std::vector<task_type> cancellation_tasks
        ( threads_created_
        , []
          {
            TRACE_LOG << "throwing last item exception";
            throw impl::last_item_exception();
          }
        )
      ;

      TRACE_LOG << "pushing last item to the queue";
      // atomically pushes all cancellation tasks
      queue_.push(cancellation_tasks.begin(), cancellation_tasks.end());

      threads_created_ = 0;

      TRACE_LOG << "waiting for all threads to finish";
      pool_.join_all();
    }

  private:
    size_t threads_created_;
    boost::thread_group pool_;
    //thread safe producer consumer queue, which uses condition variables for sync
    impl::basic_task_queue<std::function<void()>> queue_;
    std::atomic<bool> done_;
  };

我的测试用例创建并销毁了一个有 1 个线程的 thread_pool 对象,即

for(size_t i = 0; i<100; ++i)
    thread_pool pool(1);

第二次循环迭代测试用例失败,因为 done_第一次进入finish_and_join_all()true .改变 std::atomic<bool>输入 volatile bool解决了这个问题。

测试的输出如下(导致 condition_variable)失败,因为析构函数没有等待所有线程完成:

Entering test case "ctor_stress_test"
Assertion failed: (!ret), function ~condition_variable, file /usr/local/include/boost/thread/pthread/condition_variable_fwd.hpp, line 86.
[2013-07-03 22:17:32.462265] [0x76f73180] [trace]   thread_pool: creating 1 threads
[2013-07-03 22:17:32.462303] [0x76f73180] [trace]   thread_pool: created thread number: 1
[2013-07-03 22:17:32.462312] [0x76f73180] [trace]   thread_pool: all threads started
[2013-07-03 22:17:32.462320] [0x76f73180] [trace]   thread_pool: constructor finished
[2013-07-03 22:17:32.462326] [0x76f73180] [trace]   entered: finish_and_join_all & done is: false
[2013-07-03 22:17:32.462332] [0x76f73180] [trace]   pushing last item to the queue
[2013-07-03 22:17:32.462357] [0x76f73180] [trace]   waiting for all threads to finish
[2013-07-03 22:17:32.462383] [0x10d9e000] [trace]   throwing last item exception
[2013-07-03 22:17:32.462404] [0x10d9e000] [trace]   caught last item exception
[2013-07-03 22:17:32.462434] [0x76f73180] [trace]   executing ~basic_task_queue
[2013-07-03 22:17:32.462447] [0x76f73180] [trace]   thread_pool: creating 1 threads
[2013-07-03 22:17:32.462474] [0x76f73180] [trace]   thread_pool: created thread number: 1
[2013-07-03 22:17:32.462480] [0x76f73180] [trace]   thread_pool: all threads started
[2013-07-03 22:17:32.462485] [0x76f73180] [trace]   thread_pool: constructor finished
[2013-07-03 22:17:32.462490] [0x76f73180] [trace]   entered: finish_and_join_all & done is: true
[2013-07-03 22:17:32.462495] [0x76f73180] [trace]   executing ~basic_task_queue
unknown location:0: fatal error in "ctor_stress_test": signal: SIGABRT (application abort requested)
Leaving test case "ctor_stress_test"; testing time: 448mks

这是 std::atomic 的预期行为吗?

最佳答案

原子对象的初始化不是原子操作,此外,默认初始化使其处于未初始化状态(根据 §29.6.5[atomics.types.operations.req]/4)。

在构造函数的开头使用原子操作或至少定义明确的操作将其设置为 false

关于C++11,std::atomic 在 clang 3.2 和 libc++ 中损坏了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17457428/

相关文章:

c++ - 函数中输出容器默认值的开始

c++ - 退出一个函数,引用一个没有默认构造函数的类型的对象

c++ - 如何找到类模板成员函数定义的外层模板参数列表的SourceLocation?

c++11 - 使用 std::bind 与 lambda 进行绑定(bind)。它们有多贵?

c++ - OCLint ASTMatcher 规则。匹配 NS_ENUM

c - 如何指示可以使用内联ASM参数*指向*的内存?

c++ - 如何在图形问题中应用并行编程?

c++ - 您将什么称为单个 header ,其目的是包含其他 header 文件?

c++ - STL :map and mirrored data access

c++ - gcc/g++ 与 icc