2015-04-23 147 views
7

在Linux中,使用gcc 4.8.4,與-std = C++ 11 -mcx16編譯:std :: atomic load中的Segfault?

#include <atomic> 

struct node_t; 

struct pointer_t { 
     node_t* ptr; 
     unsigned int count; 
     pointer_t() noexcept : ptr{nullptr}, count{0} {} 
}; 

struct empty {}; 

struct node_t { 
     empty value; 
     std::atomic<pointer_t> next; 
     node_t() : next{pointer_t{}} {} 
}; 

int main() { 
     node_t{}.next.load(); 
     return 0; 
} 

給出了當load被稱爲段錯誤。我如何初始化原子值?

+0

就像旁註一樣,'_t'後綴是在POSIX系統上保留的。 –

+0

我沒有看到'pointer_t :: load()'的任何聲明...... – Kevin

+0

@BetaCarotin是的,我只是直接翻譯https://www.cs.rochester.edu/research/synchronization/pseudocode/ queues.html :) –

回答

8

原來這是gcc中的bug,後來在GCC 5.1中被修復。指定對齊爲兩個單詞修復它。

+0

@Drew Dormann:是否有任何補丁/解決方案可以在gcc 4.8.4本身中避免這種情況? –

+0

@ rahul.deshmukhpatil是的,您需要指定指針的對齊方式爲兩個單詞。 –

+0

我不小心改變了基類的對象地址,所以出現原子數據成員變量被分配到了奇數地址。謝謝回覆。 –

相關問題