implement a platform neutral usleep() using C++11

This commit is contained in:
Axel Kohlmeyer
2021-10-05 17:58:27 -04:00
parent 528050aa08
commit fcdabe0002
3 changed files with 22 additions and 4 deletions

View File

@ -61,7 +61,9 @@
#endif #endif
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
#include <chrono>
#include <cstring> #include <cstring>
#include <thread>
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
@ -172,6 +174,14 @@ double platform::walltime()
return wtime; return wtime;
} }
/* ----------------------------------------------------------------------
sleep with microsecond resolution
------------------------------------------------------------------------ */
void platform::usleep(int usec)
{
return std::this_thread::sleep_for(std::chrono::microseconds(usec));
}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
get Operating system and version info get Operating system and version info
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */

View File

@ -42,6 +42,15 @@ namespace platform {
double walltime(); double walltime();
/*! Suspend execution for a microsecond interval
*
* This emulates the usleep(3) BSD function call also mentioned in POSIX.1-2001.
*
* \param usec length of delay in microseconds */
void usleep(int usec);
/*! Return string with the operating system version and architecture info /*! Return string with the operating system version and architecture info
* *
* \return string with info about the OS and the platform is is running on */ * \return string with info about the OS and the platform is is running on */

View File

@ -39,7 +39,6 @@
#include <cctype> #include <cctype>
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
#include <unistd.h>
#include <unordered_map> #include <unordered_map>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
@ -692,11 +691,11 @@ int Variable::next(int narg, char **arg)
int seed = 12345 + universe->me + which[find(arg[0])]; int seed = 12345 + universe->me + which[find(arg[0])];
if (!random) random = new RanMars(lmp,seed); if (!random) random = new RanMars(lmp,seed);
int delay = (int) (1000000*random->uniform()); int delay = (int) (1000000*random->uniform());
usleep(delay); platform::usleep(delay);
while (1) { while (1) {
if (!rename("tmp.lammps.variable","tmp.lammps.variable.lock")) break; if (!rename("tmp.lammps.variable","tmp.lammps.variable.lock")) break;
delay = (int) (1000000*random->uniform()); delay = (int) (1000000*random->uniform());
usleep(delay); platform::usleep(delay);
} }
// if the file cannot be found, we may have a race with some // if the file cannot be found, we may have a race with some
@ -719,7 +718,7 @@ int Variable::next(int narg, char **arg)
break; break;
} }
delay = (int) (1000000*random->uniform()); delay = (int) (1000000*random->uniform());
usleep(delay); platform::usleep(delay);
} }
delete random; delete random;
random = nullptr; random = nullptr;