1515
1616using namespace pmem ::obj;
1717
18- /* In this example we will be using concurrent_hash_map with p<int> type for
19- * both keys and values */
18+ /* In this example we use concurrent_hash_map with p<int> type as
19+ * both keys and values. */
2020using hashmap_type = concurrent_hash_map<p<int >, p<int >>;
2121
2222const int THREADS_NUM = 30 ;
2323const bool remove_hashmap = false ;
2424
2525/* This is basic example and we only need to use concurrent_hash_map. Hence we
26- * will correlate memory pool root object with single instance of persistent
27- * pointer to hasmap_type */
26+ * correlate memory pool root object with a single instance of persistent
27+ * pointer to hashmap_type. */
2828struct root {
2929 persistent_ptr<hashmap_type> pptr;
3030};
@@ -69,7 +69,7 @@ main(int argc, char *argv[])
6969
7070 r->runtime_initialize ();
7171
72- /* defragment the whole pool at the beginning */
72+ /* Defragment the whole pool at the beginning. */
7373 try {
7474 r->defragment ();
7575 } catch (const pmem::defrag_error &e) {
@@ -84,8 +84,8 @@ main(int argc, char *argv[])
8484 std::vector<std::thread> threads;
8585 threads.reserve (static_cast <size_t >(THREADS_NUM));
8686
87- /* Insert THREADS_NUM / 3 key-value pairs to the hashmap. This
88- * operation is thread-safe. */
87+ /* Start THREADS_NUM/3 threads to insert key-value pairs
88+ * to the hashmap. This operation is thread-safe. */
8989 for (int i = 0 ; i < THREADS_NUM / 3 ; ++i) {
9090 threads.emplace_back ([&]() {
9191 for (int i = 0 ; i < 10 * THREADS_NUM; ++i) {
@@ -95,8 +95,8 @@ main(int argc, char *argv[])
9595 });
9696 }
9797
98- /* Erase THREADS_NUM /3 key-value pairs from the hashmap. This
99- * operation is thread-safe. */
98+ /* Start THREADS_NUM/3 threads to erase key-value pairs
99+ * from the hashmap. This operation is thread-safe. */
100100 for (int i = 0 ; i < THREADS_NUM / 3 ; ++i) {
101101 threads.emplace_back ([&]() {
102102 for (int i = 0 ; i < 10 * THREADS_NUM; ++i) {
@@ -105,8 +105,9 @@ main(int argc, char *argv[])
105105 });
106106 }
107107
108- /* Check if given key is in the hashmap. For the time of an
109- * accessor life, the read-write lock is taken on the item. */
108+ /* Start THREADS_NUM/3 threads to check if given key is
109+ * in the hashmap. For the time of an accessor life,
110+ * the read-write lock is taken on the item. */
110111 for (int i = 0 ; i < THREADS_NUM / 3 ; ++i) {
111112 threads.emplace_back ([&]() {
112113 for (int i = 0 ; i < 10 * THREADS_NUM; ++i) {
@@ -127,7 +128,7 @@ main(int argc, char *argv[])
127128 t.join ();
128129 }
129130 try {
130- /* defragment the whole pool at the end */
131+ /* Defragment the whole pool at the end. */
131132 map.defragment ();
132133 } catch (const pmem::defrag_error &e) {
133134 std::cerr << " Defragmentation exception: " << e.what ()
@@ -148,7 +149,7 @@ main(int argc, char *argv[])
148149 }
149150 /* Erase remaining itemes in map. This function is not
150151 * thread-safe, hence the function is being called only after
151- * thread execution has completed. */
152+ * execution of all threads has completed. */
152153 try {
153154 map.clear ();
154155 } catch (const pmem::transaction_out_of_memory &e) {
@@ -162,14 +163,14 @@ main(int argc, char *argv[])
162163 }
163164
164165 /* If hash map is to be removed, free_data() method should be
165- * called first. Otherwise, if deallocating internal hash map
166- * metadata in a destructor fail program might terminate. */
166+ * called first. Otherwise, program might terminate, if
167+ * deallocating internal hash map metadata in a destructor
168+ * fails. */
167169 if (remove_hashmap) {
168170 map.free_data ();
169171
170172 /* map.clear() // WRONG
171- * After free_data() concurrent hash map cannot be used
172- * anymore! */
173+ * After free_data() hash map cannot be used anymore! */
173174
174175 transaction::run (pop, [&] {
175176 delete_persistent<hashmap_type>(r);
0 commit comments