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 ++でビルドできますか?
が必要ですか? – Nim
@ニム、ああ、うんざり。それを逃した。どうもありがとう! –