Boost / Thread.hpp的编译错误

时间:2014-03-18 10:15:45

标签: c++ boost compiler-errors clang boost-thread

当尝试使用Boost Threading库编译一些示例C ++代码时,我得到了这个编译错误:

Insanity@MintBook ~/Desktop> clang++ btest.cpp -o btest
In file included from btest.cpp:2:
In file included from /usr/local/include/boost/thread.hpp:17:
In file included from /usr/local/include/boost/thread/once.hpp:20:
In file included from /usr/local/include/boost/thread/pthread/once_atomic.hpp:20:
In file included from /usr/local/include/boost/atomic.hpp:12:
In file included from /usr/local/include/boost/atomic/atomic.hpp:17:
In file included from /usr/local/include/boost/atomic/detail/platform.hpp:22:
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:961:64: error: no matching
      constructor for initialization of 'storage_type' (aka
      'boost::atomics::detail::storage128_type')
    explicit base_atomic(value_type const& v) BOOST_NOEXCEPT : v_(0)
                                                               ^  ~
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:932:28: note: candidate
      constructor (the implicit copy constructor) not viable: no known
      conversion from 'int' to 'const boost::atomics::detail::storage128_type'
      for 1st argument
struct BOOST_ALIGNMENT(16) storage128_type
                           ^
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:932:28: note: candidate
      constructor (the implicit default constructor) not viable: requires 0
      arguments, but 1 was provided
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:968:22: error: no viable
      conversion from 'int' to 'storage_type' (aka
      'boost::atomics::detail::storage128_type')
        storage_type tmp = 0;
                     ^     ~
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:932:28: note: candidate
      constructor (the implicit copy constructor) not viable: no known
      conversion from 'int' to 'const boost::atomics::detail::storage128_type &'
      for 1st argument
struct BOOST_ALIGNMENT(16) storage128_type
                           ^
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:983:22: error: no viable
      conversion from 'int' to 'storage_type' (aka
      'boost::atomics::detail::storage128_type')
        storage_type tmp = 0;
                     ^     ~
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:932:28: note: candidate
      constructor (the implicit copy constructor) not viable: no known
      conversion from 'int' to 'const boost::atomics::detail::storage128_type &'
      for 1st argument
struct BOOST_ALIGNMENT(16) storage128_type
                           ^
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:997:22: error: no viable
      conversion from 'int' to 'storage_type' (aka
      'boost::atomics::detail::storage128_type')
        storage_type expected_s = 0, desired_s = 0;
                     ^            ~
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:932:28: note: candidate
      constructor (the implicit copy constructor) not viable: no known
      conversion from 'int' to 'const boost::atomics::detail::storage128_type &'
      for 1st argument
struct BOOST_ALIGNMENT(16) storage128_type
                           ^
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:997:38: error: no viable
      conversion from 'int' to 'storage_type' (aka
      'boost::atomics::detail::storage128_type')
        storage_type expected_s = 0, desired_s = 0;
                                     ^           ~
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:932:28: note: candidate
      constructor (the implicit copy constructor) not viable: no known
      conversion from 'int' to 'const boost::atomics::detail::storage128_type &'
      for 1st argument
struct BOOST_ALIGNMENT(16) storage128_type
                           ^
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:1013:22: error: no viable
      conversion from 'int' to 'storage_type' (aka
      'boost::atomics::detail::storage128_type')
        storage_type expected_s = 0, desired_s = 0;
                     ^            ~
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:932:28: note: candidate
      constructor (the implicit copy constructor) not viable: no known
      conversion from 'int' to 'const boost::atomics::detail::storage128_type &'
      for 1st argument
struct BOOST_ALIGNMENT(16) storage128_type
                           ^
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:1013:38: error: no viable
      conversion from 'int' to 'storage_type' (aka
      'boost::atomics::detail::storage128_type')
        storage_type expected_s = 0, desired_s = 0;
                                     ^           ~
/usr/local/include/boost/atomic/detail/gcc-atomic.hpp:932:28: note: candidate
      constructor (the implicit copy constructor) not viable: no known
      conversion from 'int' to 'const boost::atomics::detail::storage128_type &'
      for 1st argument
struct BOOST_ALIGNMENT(16) storage128_type
                           ^
7 errors generated.

我试图编译的代码是Boost线程的一些示例代码,但是我已经确定这些错误不是这个特定源代码所独有的:

#include <iostream>
#include <boost/thread.hpp>

using namespace std;


class MyRunnable {
public:
    MyRunnable(int id, boost::mutex* mutex, boost::barrier* bar) {
        this->id    = id;
        this->mutex = mutex;
        this->bar   = bar;
    }

    // The entry point for a thread
    void operator()() {
        for(int i = 0; i < 10; ++i) {
            boost::mutex::scoped_lock  lock(*mutex);                    
                       cout << "id: " << this->id << ", " << i << endl; 
        }

        // all done, wait at the barrier. 
                // wait() returns when everyone has met at the barrier
        bar->wait();
    }

private:
    int id;
    boost::mutex* mutex;
    boost::barrier* bar;

};

int main() {
    boost::mutex io_mutex;
        // this barrier will wait for two invocations of wait()
    boost::barrier my_barrier(2); 

    cout << "Starting two counting threads..." << endl;
    // the boost::mutex cannot be copied (for obvious reasons)
    // so we must pass the pointer to the mutex.
    boost::thread thread1(MyRunnable(1, &io_mutex, &my_barrier));
    boost::thread thread2(MyRunnable(2, &io_mutex, &my_barrier));

    thread1.join(); // wait for thread1 to finish

    // Note how the program doesn't return until all threads are dead
    return 0;

}

我是C ++编程世界的新手,我不知道出了什么问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

它的a known issue,已在Boost主干中修复。