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)
/* Multithreading stuff */
#ifdef USE_SPINLOCK
spin_lock splock;
#define GUARDED_BLOCK std::lock_guard<spin_lock> lg(splock);
//#define GUARDED_BLOCK std::lock_guard<spin_lock> lg(splock);
#else
std::mutex cv_mtx;
std::condition_variable prod_cv;
......@@ -561,15 +561,15 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths)
while (1)
{
#ifdef USE_SPINLOCK
{
GUARDED_BLOCK;
if (thread_done[thread_id])
continue;
splock.lock();
int done = thread_done[thread_id];
splock.unlock();
if (done)
continue;
if (iteration_finished)
return;
}
if (iteration_finished)
return;
#else
/* 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)
/* Work for this thread finished, notify producer */
#ifdef USE_SPINLOCK
{
GUARDED_BLOCK;
thread_done[thread_id] = true;
times[thread_id] += ms.count();
}
splock.lock();
thread_done[thread_id] = 1;
times[thread_id] += ms.count();
splock.unlock();
#else
std::unique_lock<std::mutex> lck(cv_mtx);
prod_cv.notify_one();
thread_done[thread_id] = true;
thread_done[thread_id] = 1;
times[thread_id] += ms.count();
#endif /* USE_SPINLOCK */
}
......@@ -618,18 +617,19 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths)
auto start = std::chrono::high_resolution_clock::now();
#ifdef USE_SPINLOCK
{
GUARDED_BLOCK;
for (auto& td : thread_done)
td = 0;
}
splock.lock();
for (auto& td : thread_done)
td = 0;
splock.unlock();
while(1)
{
int ttd = 0;
GUARDED_BLOCK;
splock.lock();
for (auto& td : thread_done)
ttd += td;
splock.unlock();
if (ttd == nths)
break;
......@@ -666,17 +666,16 @@ double solve_multithread(wave_equation_context<T>& wec, size_t nths)
/* Tell all the threads to stop */
#ifdef USE_SPINLOCK
{
GUARDED_BLOCK;
for (size_t i = 0; i < nths; i++)
thread_done[i] = false;
iteration_finished = true;
}
splock.lock();
for (size_t i = 0; i < nths; i++)
thread_done[i] = 0;
iteration_finished = true;
splock.unlock();
#else
{
std::unique_lock<std::mutex> lck(cv_mtx);
for (size_t i = 0; i < nths; i++)
thread_done[i] = false;
thread_done[i] = 0;
iteration_finished = true;
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