1+ #ifndef _GTHREADS_
2+ #define _GTHREADS_
3+
14/*
2- GThread - multi-platform thread support
3- this is heavily based on the source code of TinyThread++ 1.0 package by Marcus Geelnard
4- (with only minor modifications and namespace changes)
5+ GThread - multi-platform threads utility class
6+ This is heavily based on the source code of TinyThread++ 1.0 package
7+ by Marcus Geelnard (with only minor modifications),
8+ so all merits for the code go to Marcus, except the bugs which
9+ were probably introduced by me.
510
6- Original Copyright notice below
11+ Original Copyright notice below
712*/
813
914/*
@@ -30,9 +35,6 @@ freely, subject to the following restrictions:
3035*/
3136
3237
33- #ifndef _GTHREADS_
34- #define _GTHREADS_
35-
3638// / @file
3739// / @mainpage TinyThread++ API Reference
3840// /
@@ -167,13 +169,6 @@ freely, subject to the following restrictions:
167169 #define GTHREADS_NO_TLS
168170#endif
169171
170-
171- // / Main name space for TinyThread++.
172- // / This namespace is more or less equivalent to the \c std namespace for the
173- // / C++11 thread classes. For instance, the tthread::mutex class corresponds to
174- // / the std::mutex class.
175- // namespace tthread {
176-
177172void gthreads_errExit (int err, const char * msg=NULL );
178173
179174#define pthreads_err (msg ) \
@@ -813,85 +808,26 @@ class GThread {
813808#endif
814809};
815810
816- // / Minimal implementation of the \c ratio class. This class provides enough
817- // / functionality to implement some basic \c chrono classes.
818- template <int64_t N, int64_t D = 1 > class ratio {
819- public:
820- static double _as_double () { return double (N) / double (D); }
821- };
822-
823- // / Minimal implementation of the \c chrono namespace.
824- // / The \c chrono namespace provides types for specifying time intervals.
825- namespace chrono {
826- // / Duration template class. This class provides enough functionality to
827- // / implement \c this_thread::sleep_for().
828- template <class _Rep , class _Period = ratio<1 > > class duration {
829- private:
830- _Rep rep_;
831- public:
832- typedef _Rep rep;
833- typedef _Period period;
834-
835- // / Construct a duration object with the given duration.
836- template <class _Rep2 >
837- explicit duration (const _Rep2& r) : rep_(r) {};
838-
839- // / Return the value of the duration object.
840- rep count () const
841- {
842- return rep_;
843- }
844- };
845-
846- // Standard duration types.
847- typedef duration<int64_t , ratio<1 , 1000000000 > > nanoseconds; // /< Duration with the unit nanoseconds.
848- typedef duration<int64_t , ratio<1 , 1000000 > > microseconds; // /< Duration with the unit microseconds.
849- typedef duration<int64_t , ratio<1 , 1000 > > milliseconds; // /< Duration with the unit milliseconds.
850- typedef duration<int64_t > seconds; // /< Duration with the unit seconds.
851- typedef duration<int64_t , ratio<60 > > minutes; // /< Duration with the unit minutes.
852- typedef duration<int64_t , ratio<3600 > > hours; // /< Duration with the unit hours.
853- }
854811
855- // / The namespace \c this_thread provides methods for dealing with the
812+ // / The namespace "current_thread" provides methods for dealing with the
856813// / calling thread.
857- namespace this_thread {
814+ namespace current_thread {
858815 // / Return the thread ID of the calling thread.
859816 // thread::id get_id(); //this can be slow, better not use it
860817
861818 // / Yield execution to another thread.
862819 // / Offers the operating system the opportunity to schedule another thread
863820 // / that is ready to run on the current processor.
864- inline void yield ()
865- {
866- #if defined(_GTHREADS_WIN32_)
867- Sleep (0 );
868- #else
869- sched_yield ();
870- #endif
871- }
872-
873- // / Blocks the calling thread for a period of time.
874- // / @param[in] aTime Minimum time to put the thread to sleep.
875- // / Example usage:
876- // / @code
877- // / // Sleep for 100 milliseconds
878- // / this_thread::sleep_for(chrono::milliseconds(100));
879- // / @endcode
880- // / @note Supported duration types are: nanoseconds, microseconds,
881- // / milliseconds, seconds, minutes and hours.
882- template <class _Rep , class _Period > void sleep_for (const chrono::duration<_Rep, _Period>& aTime)
883- {
884- #if defined(_GTHREADS_WIN32_)
885- Sleep (int (double (aTime.count ()) * (1000.0 * _Period::_as_double ()) + 0.5 ));
886- #else
887- usleep (int (double (aTime.count ()) * (1000000.0 * _Period::_as_double ()) + 0.5 ));
888- #endif
889- }
821+ void yield ();
822+
823+ // Blocks the calling thread for a certain time (given in milliseconds)
824+ // Example usage:
825+ // // Sleep for 100 milliseconds:
826+ // current_thread::sleep_for(100);
827+ void sleep_for (const int32_t mstime);
890828}
891829
892830// Define/macro cleanup
893831#undef _GTHREADS_DISABLE_ASSIGNMENT
894832
895-
896-
897833#endif // _GTHREADS_
0 commit comments