Die cppreference-Website verfügt über eine (work in progress) page describing transactional memory c++ code. Dies ist das erste Beispiel auf der SeiteErstellen von Transaktionsspeicher C++ - Code in g ++
#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();
}
zum unteren Teil dieser Seite gibt es eine Anzeige, daß dies auf gcc baut (// GCC assembly with the attribute:
).
Ich kann das nicht auf g aufzubauen erhalten ++ 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 Dokumentation tut haben a page on transactional memory, aber die Grundelemente unterschiedlich sind (zum Beispiel das Atom Block ist __transaction_atomic
). Die Seite auf cppreference.com scheint umgekehrt mit N3919 verwandt zu sein und verwendet die Grundelemente von dort.
Wie kann dieser Code mit g ++ erstellt werden?
an der Unterseite es sagt, Sie gcc 6.1 verwenden? – Nim
@Nim, Ooh, Mist - verpasst das. Danke vielmals! –