8
8
#include < numeric>
9
9
#include < atomic>
10
10
#include < iomanip>
11
+ #include < deque>
11
12
12
13
using namespace std ;
13
14
@@ -31,7 +32,7 @@ class Progress {
31
32
32
33
void do_random_walk (const int thread_id, const int size, const int dimension,
33
34
const unsigned int max_try_before_giveup, const int MODULO, int & oWent_back_to_start,
34
- std::atomic<Progress*>* progress)
35
+ std::atomic<int >& progress)
35
36
{
36
37
std::random_device rd; // obtain a random number from hardware
37
38
std::mt19937 gen (rd ()); // seed the generator
@@ -56,10 +57,8 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
56
57
if (percent >= nextPrint)
57
58
{
58
59
// std::cout << "Thread progress(" << thread_id << "): " << std::string(percent/5, '|') << percent << "%" << std::endl;
59
- Progress* aProgress = progress->load ();
60
- aProgress->_percent =percent;
61
- progress->store (aProgress);
62
- progress->notify_all ();
60
+ progress = percent;
61
+ progress.notify_one ();
63
62
nextPrint += step;
64
63
}
65
64
@@ -97,40 +96,26 @@ void do_random_walk(const int thread_id, const int size, const int dimension,
97
96
98
97
if (went_back_to_start) {
99
98
oWent_back_to_start++;
100
- /* Progress aProgress = progress->load();
101
- aProgress._went_back_to_start++;
102
- progress->store(aProgress);*/
103
99
break ;
104
100
}
105
101
}
106
102
}
107
103
108
- Progress* aProgress = progress->load ();
109
- aProgress->_percent = 100 ;
110
- progress->store (aProgress);
104
+ progress=100 ;
111
105
112
106
}
113
107
114
- void monitor_progress (std::vector <std::atomic<Progress*>*>* progress, int size){
108
+ void monitor_progress (std::deque <std::atomic<int >>& progress, int size){
115
109
bool keep = true ;
116
110
float barWidth = 15.0 ;
117
111
while (keep){
118
- chrono::milliseconds dura ( 2000 );
112
+ chrono::milliseconds dura ( 100 );
119
113
this_thread::sleep_for (dura); // Sleeps for a bit
120
114
keep = false ;
121
- cout << " \r " ;
115
+ cout << " \r " ;
122
116
123
- /* int total_success = 0;
124
- for (int i = 0; i < progress->size(); i++) {
125
- total_success+=(*progress)[i]->load()->_went_back_to_start;
126
- }
127
-
128
- cout << "[F=" << std::fixed << setprecision(1) << std::setw(4) << (100.0*(size-total_success))/size << "%]" ;*/
129
-
130
-
131
-
132
- for (int i = 0 ; i < progress->size (); i++) {
133
- int l_progress = (*progress)[i]->load ()->_percent ;
117
+ for (int i = 0 ; i < progress.size (); i++) {
118
+ int l_progress = progress[i];
134
119
cout << " T#" << i << " :[" ;
135
120
int pos = l_progress * (barWidth / 100 );
136
121
for (int w = 0 ; w < barWidth; ++w) {
@@ -155,11 +140,11 @@ void monitor_progress(std::vector<std::atomic<Progress*>*>* progress, int size){
155
140
int main ()
156
141
{
157
142
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now ();
158
- unsigned int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<unsigned int >::max ();
143
+ unsigned int MAX_TRY_BEFORE_GIVEUP = std::numeric_limits<unsigned int >::max () ;
159
144
int DIMENSION = 2 ;
160
145
int NUMBER_OF_RDM_WALKS = 100 ;
161
146
int MODULO = 0 ;
162
- int THREAD_NUMBER = 4 ;
147
+ int THREAD_NUMBER = 10 ;
163
148
cout << " MAX_TRY_BEFORE_GIVEUP=" << MAX_TRY_BEFORE_GIVEUP << endl;
164
149
cout << " DIMENSION=" << DIMENSION << endl;
165
150
cout << " NUMBER_OF_RDM_WALKS=" << NUMBER_OF_RDM_WALKS << endl;
@@ -174,16 +159,18 @@ int main()
174
159
std::vector<std::thread> threads (THREAD_NUMBER);
175
160
std::vector<int > went_back_count (THREAD_NUMBER);
176
161
177
- std::vector <std::atomic<Progress*>* > cProgress;
162
+ std::deque <std::atomic<int > > cProgress;
178
163
179
164
// spawn n threads:
180
165
for (int i = 0 ; i < THREAD_NUMBER; i++) {
181
166
went_back_count[i] = 0 ;
182
- cProgress.emplace_back (new std::atomic<Progress*>( new Progress ()) );
183
- threads[i] = std::thread (do_random_walk, i, NUMBER_OF_RDM_WALKS/THREAD_NUMBER, DIMENSION, MAX_TRY_BEFORE_GIVEUP, MODULO, std::ref (went_back_count[i]), cProgress[i] );
167
+ cProgress.emplace_back (0 );
168
+ threads[i] = std::thread (do_random_walk, i, NUMBER_OF_RDM_WALKS/THREAD_NUMBER, DIMENSION, MAX_TRY_BEFORE_GIVEUP, MODULO, std::ref (went_back_count[i]), std::ref ( cProgress[i]) );
184
169
}
185
170
186
- monitor_progress (&cProgress, REAL_NUMBER_OF_RDM_WALKS);
171
+
172
+
173
+ monitor_progress (std::ref (cProgress), REAL_NUMBER_OF_RDM_WALKS);
187
174
188
175
for (int i = 0 ; i < THREAD_NUMBER; i++) {
189
176
threads[i].join ();
0 commit comments