2016-08-05 9 views
1

cppreferenceウェブサイトは(work in progress) page describing transactional memory c++ codeです。これはページそのページの底部に向かってトランザクションメモリの構築g ++でのC++コード

#include <iostream> 
#include <vector> 
#include <thread> 
int f() 
{ 
    static int i = 0; 
    synchronized { // begin synchronized block 
     std::cout << i << " -> "; 
     ++i;  // each call to f() obtains a unique value of i 
     std::cout << i << '\n'; 
     return i; // end synchronized block 
    } 
} 
int main() 
{ 
    std::vector<std::thread> v(10); 
    for(auto& t: v) 
     t = std::thread([]{ for(int n = 0; n < 10; ++n) f(); }); 
    for(auto& t: v) 
     t.join(); 
} 

の最初の例であり、これはGCC(// GCC assembly with the attribute:)上に構築ことの指標です。

私がg ++ 5.3.1で構築することができません。

$ g++ --std=c++11 -fgnu-tm -lpthread trx.cpp 
trx.cpp: In function ‘int f()’: 
trx.cpp:7:5: error: ‘synchronized’ was not declared in this scope 
    synchronized { // begin synchronized block 
    ^

$ g++ --help | grep transaction 

$ g++ --version 
g++ (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413 

gccのドキュメントa page on transactional memoryを持っていますが、プリミティブ(例えば、アトミック・ブロックが__transaction_atomicある)が異なるん。 cppreference.comのページは逆にN3919に関連しているようで、そこからのプリミティブを使用します。

このコードはどのようにg ++でビルドできますか?

+0

が必要ですか? – Nim

+0

@ニム、ああ、うんざり。それを逃した。どうもありがとう! –

答えて

4

あなたが最初に言及transactional_memoryのリンクは言う:

コンパイラのサポート

この技術仕様書は、バージョン6.1のようGCCによってサポートされています(有効にする-fgnu-tmが必要です)。

は、だから、それはあなたが、GCC 6.1を使用する必要が言う下部に(-fgnu-tmに加えて、おそらくalso-std=c++1z ....)GCC 6

+0

多くの感謝 - 私は6.1の要件を逃した。 –

関連する問題