c++ 11原子.为什么这个编译,而不是链接
C++11 Atomics. Why does this compile, but not link?
我正在尝试编写一个多生产者,多消费者队列。我在Arch Linux上使用g++ 4.6,它在g++ 4.7上也坏了。
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
template <typename T> class concurrent_queue
{
public:
concurrent_queue(size_t n) : items(n), item_states(n), producer_index(0), consumer_index(0) {
}
virtual ~concurrent_queue () {
}
T * consume() {
auto index = consumer_index;
T * item = nullptr;
state_t state = USED;
// Extract item and block location.
while (!item_states[index].compare_exchange_strong(state, BLOCKED, std::memory_order_acquire, std::memory_order_acquire)) {
// Wait for an item to become available
std::unique_lock<std::mutex> lock(item_mutex);
has_items.wait(lock);
// Move to the newly available item (might already have been taken by another consumer).
index = consumer_index;
}
// Tell consumers to move to next location, wrap to beginning of circular buffer if necessary.
++consumer_index;
consumer_index %= items.size();
// Unblock index so that producers can write to it.
items[index] = nullptr;
return item;
}
void produce(T * value) {
items[producer_index] = value;
++producer_index;
producer_index %= items.size();
has_items.notify_one();
}
private:
typedef enum {
EMPTY,
USED,
BLOCKED
} state_t;
// Used as a buffer of items
std::vector<T* > items;
std::vector<std::atomic<state_t> > item_states;
size_t producer_index;
size_t consumer_index;
std::mutex item_mutex;
std::condition_variable has_items;
};
// Test code
using namespace std;
template <typename T> void pop_n_print(concurrent_queue<T> & queue) {
stringstream message;
message << "popped " << *queue.consume() << endl;
cout << message.str();
}
int main ()
{
concurrent_queue<int> ints(5);
ints.produce(new int(1));
ints.produce(new int(2));
pop_n_print(ints);
pop_n_print(ints);
return 0;
}
我用g++ --std=c++0x queue.cc -o test_queue
编译这段代码,但我得到这个错误消息:
/tmp/ccqCjADk.o: In function `concurrent_queue<int>::consume()':
queue.cc:(.text._ZN16concurrent_queueIiE7consumeEv[concurrent_queue<int>::consume()]+0x9f): undefined reference to `std::atomic<concurrent_queue<int>::state_t>::compare_exchange_strong(concurrent_queue<int>::state_t&, concurrent_queue<int>::state_t, std::memory_order, std::memory_order)'
collect2: ld returned 1 exit status
我不确定为什么会发生这种情况,但它似乎表明我需要链接到一些东西。你知道我哪里做错了吗?
因为std::atomic只存在于特定的t,主要是数字类型。
相关文章:
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- Red Hat:使用<atomic>编译很好,但链接器找不到__atomic_store_16;什么库?
- 在发布模式下启动使用库的静态链接编译的应用程序时出错
- 编译时出错 - 链接.cpp和头文件
- 仅为从某种语言编译的对象添加链接库?
- C++ 为什么在定义的编译和链接之前引用外部实例的程序
- Visual Studio 2017 ARM 交叉编译链接器崩溃
- 在调试配置中编译工作正常,但发布会给出链接错误
- 可视化工作室项目中C++预编译标头未正确链接
- std::to_chars在MacOS/clang上编译但不链接
- 使用共享库编译可执行文件时仅链接所需的符号
- 编译时出现Boost.python链接错误
- 微控制器的首次 gcc 链接器脚本编译但不运行
- CUDA 和C++链接/编译,程序在 cudaMalloc 上崩溃
- 链接/编译时间有关静态模板库
- 链接/编译使用 boost/filesystem.hpp 的程序
- 我们可以动态链接编译的静态库吗?
- 如何在我的项目中使用CMake链接编译的cmph库
- 在msvc++ 2010中,在调试配置中用静态链接编译DLL时,链接器错误