Skip to content
Snippets Groups Projects
Commit 37fc983f authored by Matteo Cicuttin's avatar Matteo Cicuttin
Browse files

Spinlock without lock_guard.

parent f0d863ea
No related branches found
No related tags found
No related merge requests found
...@@ -543,7 +543,7 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) ...@@ -543,7 +543,7 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths)
/* Multithreading stuff */ /* Multithreading stuff */
#ifdef USE_SPINLOCK #ifdef USE_SPINLOCK
spin_lock splock; spin_lock splock;
#define GUARDED_BLOCK std::lock_guard<spin_lock> lg(splock); //#define GUARDED_BLOCK std::lock_guard<spin_lock> lg(splock);
#else #else
std::mutex cv_mtx; std::mutex cv_mtx;
std::condition_variable prod_cv; std::condition_variable prod_cv;
...@@ -561,15 +561,15 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) ...@@ -561,15 +561,15 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths)
while (1) while (1)
{ {
#ifdef USE_SPINLOCK #ifdef USE_SPINLOCK
{ splock.lock();
GUARDED_BLOCK; int done = thread_done[thread_id];
if (thread_done[thread_id]) splock.unlock();
if (done)
continue; continue;
if (iteration_finished) if (iteration_finished)
return; return;
}
#else #else
/* Wait for the producer to notify that there's something to do */ /* Wait for the producer to notify that there's something to do */
{ {
...@@ -590,15 +590,14 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) ...@@ -590,15 +590,14 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths)
/* Work for this thread finished, notify producer */ /* Work for this thread finished, notify producer */
#ifdef USE_SPINLOCK #ifdef USE_SPINLOCK
{ splock.lock();
GUARDED_BLOCK; thread_done[thread_id] = 1;
thread_done[thread_id] = true;
times[thread_id] += ms.count(); times[thread_id] += ms.count();
} splock.unlock();
#else #else
std::unique_lock<std::mutex> lck(cv_mtx); std::unique_lock<std::mutex> lck(cv_mtx);
prod_cv.notify_one(); prod_cv.notify_one();
thread_done[thread_id] = true; thread_done[thread_id] = 1;
times[thread_id] += ms.count(); times[thread_id] += ms.count();
#endif /* USE_SPINLOCK */ #endif /* USE_SPINLOCK */
} }
...@@ -618,18 +617,19 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) ...@@ -618,18 +617,19 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths)
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
#ifdef USE_SPINLOCK #ifdef USE_SPINLOCK
{ splock.lock();
GUARDED_BLOCK;
for (auto& td : thread_done) for (auto& td : thread_done)
td = 0; td = 0;
} splock.unlock();
while(1) while(1)
{ {
int ttd = 0; int ttd = 0;
GUARDED_BLOCK;
splock.lock();
for (auto& td : thread_done) for (auto& td : thread_done)
ttd += td; ttd += td;
splock.unlock();
if (ttd == nths) if (ttd == nths)
break; break;
...@@ -666,17 +666,16 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths) ...@@ -666,17 +666,16 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths)
/* Tell all the threads to stop */ /* Tell all the threads to stop */
#ifdef USE_SPINLOCK #ifdef USE_SPINLOCK
{ splock.lock();
GUARDED_BLOCK;
for (size_t i = 0; i < nths; i++) for (size_t i = 0; i < nths; i++)
thread_done[i] = false; thread_done[i] = 0;
iteration_finished = true; iteration_finished = true;
} splock.unlock();
#else #else
{ {
std::unique_lock<std::mutex> lck(cv_mtx); std::unique_lock<std::mutex> lck(cv_mtx);
for (size_t i = 0; i < nths; i++) for (size_t i = 0; i < nths; i++)
thread_done[i] = false; thread_done[i] = 0;
iteration_finished = true; iteration_finished = true;
cons_cv.notify_all(); cons_cv.notify_all();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment