From 4a9eedd4daf0dbaa3567516275932d16b2ec0f85 Mon Sep 17 00:00:00 2001 From: kronbichler Date: Fri, 17 Apr 2009 09:50:18 +0000 Subject: [PATCH] Make TimerOutput really thread-safe. git-svn-id: https://svn.dealii.org/trunk@18639 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/timer.h | 9 +++++++++ deal.II/base/source/timer.cc | 18 +++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/deal.II/base/include/base/timer.h b/deal.II/base/include/base/timer.h index ada33f4056..ca90e4f765 100644 --- a/deal.II/base/include/base/timer.h +++ b/deal.II/base/include/base/timer.h @@ -15,6 +15,7 @@ #include #include +#include #ifdef DEAL_II_COMPILER_SUPPORTS_MPI #include @@ -370,6 +371,14 @@ class TimerOutput */ MPI_Comm mpi_communicator; #endif + + /** + * A lock that makes sure that this + * class gives reasonable results even + * when used with several threads. + */ + + Threads::ThreadMutex mutex; }; DEAL_II_NAMESPACE_CLOSE diff --git a/deal.II/base/source/timer.cc b/deal.II/base/source/timer.cc index b442e66345..48d91776a6 100644 --- a/deal.II/base/source/timer.cc +++ b/deal.II/base/source/timer.cc @@ -22,8 +22,6 @@ #include #include -#include - // on SunOS 4.x, getrusage is stated in the man pages and exists, but // is not declared in resource.h. declare it ourselves @@ -272,8 +270,7 @@ TimerOutput::~TimerOutput() void TimerOutput::enter_section (const std::string §ion_name) { - Threads::Mutex mutex; - Threads::Mutex::ScopedLock lock (mutex); + Threads::ThreadMutex::ScopedLock lock (mutex); Assert (section_name.empty() == false, ExcMessage ("Section string is empty.")); @@ -301,8 +298,7 @@ TimerOutput::enter_section (const std::string §ion_name) void TimerOutput::exit_section (const std::string §ion_name) { - Threads::Mutex mutex; - Threads::Mutex::ScopedLock lock (mutex); + Threads::ThreadMutex::ScopedLock lock (mutex); if (section_name != "") { @@ -430,7 +426,7 @@ TimerOutput::print_summary () std::cout.width(10); std::cout.precision(3); out_stream << " CPU time " << " | % of total |\n"; - out_stream << "+---------------------------------------------+------------" + out_stream << "+---------------------------------+-----------+------------" << "+------------+"; for (std::map::const_iterator i = sections.begin(); i!=sections.end(); ++i) @@ -455,7 +451,7 @@ TimerOutput::print_summary () out_stream << i->second.total_cpu_time/total_cpu_time * 100 << "% |"; } out_stream << std::endl - << "+---------------------------------------------+" + << "+---------------------------------+-----------+" << "------------+------------+\n" << std::endl; @@ -488,7 +484,7 @@ TimerOutput::print_summary () out_stream << "\n\n" << "+---------------------------------------------+------------" << "+------------+\n" - << "| Total wallclock time elapsed from start |"; + << "| Total wallclock time elapsed since start |"; std::cout.width(10); std::cout.precision(3); out_stream << total_wall_time << "s | |\n"; @@ -498,7 +494,7 @@ TimerOutput::print_summary () std::cout.width(10); std::cout.precision(3); out_stream << " wall time | % of total |\n"; - out_stream << "+---------------------------------------------+------------" + out_stream << "+---------------------------------+-----------+------------" << "+------------+"; for (std::map::const_iterator i = sections.begin(); i!=sections.end(); ++i) @@ -523,7 +519,7 @@ TimerOutput::print_summary () out_stream << i->second.total_wall_time/total_wall_time * 100 << "% |"; } out_stream << std::endl - << "+---------------------------------------------+" + << "+---------------------------------+-----------+" << "------------+------------+\n" << std::endl; -- 2.39.5