Ignore:
Timestamp:
Jun 19, 2017, 8:23:19 AM (8 years ago)
Author:
Frederik Heber <frederik.heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_StructOpt_integration_tests, AutomationFragmentation_failures, Candidate_v1.6.1, ChangeBugEmailaddress, ChemicalSpaceEvaluator, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph_documentation, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, Fix_ChronosMutex, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, ForceAnnealing_oldresults, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, GeometryObjects, Gui_displays_atomic_force_velocity, IndependentFragmentGrids_IntegrationTest, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, RotateToPrincipalAxisSystem_UndoRedo, StoppableMakroAction, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps
Children:
61003d, 788dce, be848d
Parents:
51a013
git-author:
Frederik Heber <frederik.heber@…> (06/19/17 08:23:15)
git-committer:
Frederik Heber <frederik.heber@…> (06/19/17 08:23:19)
Message:

Chronos can now be safely used in multithread environements.

  • this fixes a bug with JobMarket where JobMarketPoolWorker would crash because handle_ReceiveJobs and Work would both try to access Chronos.
Location:
ThirdParty/CodePatterns/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ThirdParty/CodePatterns/src/CodePatterns/Chronos.hpp

    r51a013 r9a9f847  
    1717#include <map>
    1818#include <string>
     19
     20#include <boost/thread/recursive_mutex.hpp>
    1921
    2022#include "CodePatterns/Singleton.hpp"
     
    131133#endif
    132134#endif
     135
     136  //!> mutex for keeping chronos timekeeping atomic
     137  mutable boost::recursive_mutex ChronosMutex;
    133138};
    134139
  • ThirdParty/CodePatterns/src/Helpers/Chronos.cpp

    r51a013 r9a9f847  
    2222#include <iostream>
    2323
     24#include <boost/thread/locks.hpp>
     25
    2426#ifdef HAVE_UNISTD_H
    2527#include <unistd.h>
     
    3234# include <time.h>
    3335#endif
    34 
    3536
    3637#include "CodePatterns/Chronos.hpp"
     
    6970double Chronos::getTime(const std::string &_name) const
    7071{
     72  boost::recursive_mutex::scoped_lock lock(ChronosMutex);
    7173  // only those functions have a time that have run already
    7274  if (AccountedTime.count(_name) != 0) {
     
    8284void Chronos::resetTime(const std::string &_name)
    8385{
     86  boost::recursive_mutex::scoped_lock lock(ChronosMutex);
    8487  // set accounted time to zero
    8588  if (AccountedTime.count(_name) != 0) {
     
    9396void Chronos::startTiming(const std::string &_name)
    9497{
     98  boost::recursive_mutex::scoped_lock lock(ChronosMutex);
    9599  // start time keeping
    96100  if ((RecursionMap.count(_name) == 0) || (RecursionMap[_name] == 0)) {
     
    106110    const sec_ncsec_t &_time2)
    107111{
    108         double currenttime = 0.;
     112  double currenttime = 0.;
    109113  if (_time1.second < _time2.second)
    110114                currenttime = (_time1.first - _time2.first - 1)
     
    118122double Chronos::getCurrentTime() const
    119123{
     124  boost::recursive_mutex::scoped_lock lock(ChronosMutex);
    120125#ifdef HAVE_TIME_H
    121126  // clock_gettime gives nanoseconds accuracy
     
    158163void Chronos::endTiming(const std::string &_name)
    159164{
     165  boost::recursive_mutex::scoped_lock lock(ChronosMutex);
    160166  // check whether we are the topmost function, return if not
    161167  if (--RecursionMap[_name] != 0)
     
    183189double Chronos::SumUpTotalTime() const
    184190{
     191  boost::recursive_mutex::scoped_lock lock(ChronosMutex);
    185192  double sum = 0.;
    186193  for (TimekeepingMap::const_iterator iter = AccountedTime.begin();
     
    194201size_t Chronos::SumUpTotalFunctions() const
    195202{
     203  boost::recursive_mutex::scoped_lock lock(ChronosMutex);
    196204  return AccountedTime.size();
    197205}
     
    199207std::ostream& operator<<(std::ostream &ost, const Chronos &_time)
    200208{
     209  boost::recursive_mutex::scoped_lock lock(_time.ChronosMutex);
    201210  ost << "List of functions present:" << std::endl;
    202211  for (Chronos::TimekeepingMap::const_iterator iter = _time.AccountedTime.begin();
Note: See TracChangeset for help on using the changeset viewer.