Allolib  1.0
C++ Components For Interactive Multimedia
al_PeriodicThread.hpp
1 #ifndef INCLUDE_AL_PERIODIC_THREAD_HPP
2 #define INCLUDE_AL_PERIODIC_THREAD_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  Thread that calls a function periodically
42 
43  File author(s):
44  Lance Putnam, 2013, putnam.lance@gmail.com
45 */
46 #include <functional>
47 
48 #include "al/system/al_Thread.hpp"
49 #include "al/system/al_Time.hpp"
50 
51 namespace al {
53 
55 
61 class PeriodicThread : public Thread {
62  public:
68  PeriodicThread(double periodSec = 1);
69 
72 
74 
84  PeriodicThread& autocorrect(float factor);
85 
87  PeriodicThread& period(double sec);
88 
90  double period() const;
91 
93  void start(ThreadFunction& func);
94 
96  void start(std::function<void(void* data)> function,
97  void* userData = nullptr);
98 
100  void stop();
101 
103  bool running() const;
104 
105  // Stuff for assignment
106  friend void swap(PeriodicThread& a, PeriodicThread& b);
107  PeriodicThread& operator=(PeriodicThread other);
108 
109  private:
110  static void* sPeriodicFunc(void* userData);
111  void go();
112 
113  al_nsec mPeriod;
114  al_nsec mTimeCurr, mTimePrev; // time measurements between frames
115  al_nsec mWait; // actual time to sleep between frames
116  al_nsec mTimeBehind;
117  float mAutocorrect;
118  ThreadFunction* mUserFunc;
119  void* mUserData;
120  std::function<void(void* data)> mFunction;
121  bool mRun;
122 };
123 
124 } // namespace al
125 
126 #endif
Thread that calls a function periodically.
PeriodicThread & autocorrect(float factor)
Set autocorrection factor.
PeriodicThread(double periodSec=1)
PeriodicThread & period(double sec)
Set period, in seconds.
void stop()
Stop the thread.
void start(std::function< void(void *data)> function, void *userData=nullptr)
Start calling the supplied function periodically.
PeriodicThread(const PeriodicThread &other)
Copy constructor.
void start(ThreadFunction &func)
Start calling the supplied function periodically.
double period() const
Get period, in seconds.
bool running() const
True if timer thread is running.
Definition: al_App.hpp:23
Function object interface used by thread.
Definition: al_Thread.hpp:51