2013-02-10 134 views
2

我一直在尋找的C++實現利用原子能無等待隊列,發現Boost.Atomic例如:無等待隊列崩潰

template<typename T> 
class waitfree_queue { 
public: 
    struct node { 
    T data; 
    node * next; 
    }; 
    void push(const T &data) 
    { 
    node * n = new node; 
    n->data = data; 
    node * stale_head = head_.load(boost::memory_order_relaxed); 
    do { 
     n->next = stale_head; 
    } while (!head_.compare_exchange_weak(stale_head, n, boost::memory_order_release)); 
    } 

    node * pop_all(void) 
    { 
    T * last = pop_all_reverse(), * first = 0; 
    while(last) { 
     T * tmp = last; 
     last = last->next; 
     tmp->next = first; 
     first = tmp; 
    } 
    return first; 
    } 

    waitfree_queue() : head_(0) {} 

    // alternative interface if ordering is of no importance 
    node * pop_all_reverse(void) 
    { 
    return head_.exchange(0, boost::memory_order_consume); 
    } 
private: 
    boost::atomic<node *> head_; 
}; 

int main() { 
// pop elements 
waitfree_queue<int>::node * x = q.pop_all() 
while(x) { 
    X * tmp = x; 
    x = x->next; 
    // process tmp->data, probably delete it afterwards 
    delete tmp; 
} 

} 

Example at boost official site

我已經更換了提升性病與MSVC 2012年 編譯它崩潰與下一條消息在控制檯:

Assertion failed: _Order2 != memory_order_release, file c:\program files (x86)\m 
icrosoft visual studio 11.0\vc\include\xxatomic, line 742 

當我編譯原升壓運行W/A崩潰。

它是在Boost.Atomic或MSVC實現原子的bug嗎?

回答

1

它看起來像MSVC實施中的錯誤。斷言失敗,因爲Order2memory_order_release。然而,如所指出here(其是相同的C++標準)(重點煤礦):

的3參數超負荷等同於4-參數超載 與success_order ==順序,和failure_order ==除了如果 命令是std :: memory_order_acq_rel,那麼failure_order是 std :: memory_order_acquire和如果命令是std :: memory_order_release 那麼failure_order是std :: memory_order_relaxed

換句話說,Order2必須std::memory_order_relaxed針對你的情況4-參數超載,因爲你通過memory_order_releaseorder。在MSVC的實現中這不是一個錯誤。如果可能,請將其報告爲錯誤。