Boost/Thread.hpp编译错误

Compilation error with Boost/Thread.hpp

本文关键字:编译 错误 hpp Thread Boost      更新时间:2023-10-16

当尝试使用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++编程世界的新手,我不知道哪里出了问题。

这是一个已知的问题,在Boost主干中修复。