137
Listing 8-13. Iterating over persistent container and compatibility with STD
algorithms
95
std::vector stdvector = {5, 4, 3, 2, 1};
96
pvector = stdvector;
97
98
try {
99
pmem::obj::transaction::run(pool, [&] {
100
for (auto &e : pvector)
101
e++;
102
/* 6, 5, 4, 3, 2 */
103
104
for (auto it = pvector.begin();
it != pvector.end(); it++)
105
*it += 2;
106
/* 8, 7, 6, 5, 4 */
107
108
for (unsigned i = 0; i < pvector.size(); i++)
109
pvector[i]--;
110
/* 7, 6, 5, 4, 3 */
111
112
std::sort(pvector.begin(), pvector.end());
113
for (unsigned i = 0; i < pvector.size(); ++i)
114
assert(pvector.const_at(i) == static_cast
(i + 3));
115
116
pmem::obj::transaction::abort(0);
117
});
118
} catch (pmem::manual_tx_abort &) {
119
/* expected transaction abort */
120
} catch (std::exception &e) {
121
std::cerr << e.what() << std::endl;
122
}
123
Chapter 8 libpmemobj-Cpp: the adaptable language - C++ and persistent memory
Précédent

- 163/457

Suivant