SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
thread.cpp
Go to the documentation of this file.
1 #include "thread.h"
2 #include <time.h>
3 
4 
5 namespace CVD {
6 //Static variables
7 bool Thread::ourInitializedFlag = false;
8 pthread_key_t Thread::ourKey;
9 unsigned int Thread::ourCount;
10 
12 Thread::Thread() : myRunningFlag(false), myStopFlag(false)
13 {
14  if (!ourInitializedFlag)
15  {
16  init();
17  ourInitializedFlag = true;
18  }
19 }
20 
23 {
24  if (isRunning())
25  {
26  stop();
27  join();
28  }
29 }
30 
32 void Thread::start(Runnable* runnable)
33 {
34  myRunnable = runnable ? runnable : this;
35  myStopFlag = false;
36  pthread_attr_t attr;
37  pthread_attr_init(&attr);
38  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
39  ourCount++;
40  pthread_create(&myID, &attr, threadproc, this);
41 }
42 
43 void Thread::stop()
44 {
45  myStopFlag = true;
46 }
47 
48 bool Thread::shouldStop() const
49 {
50  return myStopFlag;
51 }
52 
53 bool Thread::isRunning() const
54 {
55  return myRunningFlag;
56 }
57 
58 void Thread::join()
59 {
60  pthread_join(myID,0);
61 }
62 
63 pthread_t Thread::getID()
64 {
65  return myID;
66 }
67 
68 unsigned int Thread::count()
69 {
70  return ourCount;
71 }
72 
74 {
75  return (Thread*)pthread_getspecific(ourKey);
76 }
77 
79 void Thread::sleep(unsigned int milli)
80 {
81  struct timespec ts = { milli/1000, (milli%1000)*1000000 };
82  nanosleep(&ts, 0);
83 
84 }
85 
86 //void Thread::yield()
87 //{
88 // pthread_yield_np();
89 //}
90 
91 bool Thread::init()
92 {
93  ourCount = 0;
94  pthread_key_create(&ourKey,0);
95  return true;
96 }
97 
98 void* Thread::threadproc(void* param)
99 {
100  Thread* thread = (Thread*)param;
101  pthread_setspecific(ourKey, thread);
102  thread->myRunningFlag = true;
103  if (thread->myRunnable)
104  thread->myRunnable->run();
105  else
106  thread->run();
107  thread->myRunningFlag = false;
108  ourCount--;
109  return 0;
110 }
111 
112 }
Thread()
Construct a thread. If runnable != 0, use that runnable, else use our own "run" method.
Definition: thread.cpp:12
static bool init()
Definition: thread.cpp:91
pthread_t myID
Definition: thread.h:66
virtual void run()
Override this method to do whatever it is the thread should do.
Definition: thread.h:43
void stop()
Tell the thread to stop.
Definition: thread.cpp:43
virtual void run()=0
Perform the function of this object.
static unsigned int count()
Returns how many threads are actually running, not including the main thread.
Definition: thread.cpp:68
This is an abstract base class for anything with a run() method.
Definition: runnable.h:6
Definition: abs.h:24
Runnable * myRunnable
Definition: thread.h:65
void start(Runnable *runnable=0)
Start execution of "run" method in separate thread.
Definition: thread.cpp:32
static Thread * getCurrent()
Returns a pointer to the currently running thread.
Definition: thread.cpp:73
static void * threadproc(void *param)
Definition: thread.cpp:98
bool isRunning() const
Returns true if the thread is still running.
Definition: thread.cpp:53
static bool ourInitializedFlag
Definition: thread.h:61
static pthread_key_t ourKey
Definition: thread.h:63
pthread_t getID()
Get the ID of this thread.
Definition: thread.cpp:63
bool myStopFlag
Definition: thread.h:68
bool myRunningFlag
Definition: thread.h:67
bool shouldStop() const
Returns true if the stop() method been called, false otherwise.
Definition: thread.cpp:48
static unsigned int ourCount
Definition: thread.h:64
virtual ~Thread()
This does not destroy the object until the thread has been terminated.
Definition: thread.cpp:22
void join()
This blocks until the thread has actually terminated.
Definition: thread.cpp:58
static void sleep(unsigned int milli)
Tell the current thread to sleep for milli milliseconds.
Definition: thread.cpp:79