-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaitCursor.cpp
64 lines (55 loc) · 1.23 KB
/
WaitCursor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "WaitCursor.h"
#include <sys/time.h>
#include <iostream>
WaitCursor::WaitCursor(bool start) :
quit(false)
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
if(start)
{
this->start();
}
}
WaitCursor::~WaitCursor()
{
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
bool WaitCursor::start(float dlay)
{
delay = dlay;
return pthread_create(&tid, NULL, thread_, this) == 0;
}
void WaitCursor::stop()
{
pthread_mutex_lock(&mutex);
quit = true;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
pthread_join(tid, NULL);
tid = 0;
quit = false;
}
void *WaitCursor::thread()
{
const long BILLION = 1000000000L;
int x = 0;
struct timeval now;
long dlay = (long)(BILLION * delay);
while(true)
{
gettimeofday(&now, NULL);
struct timespec timeout = { now.tv_sec, (now.tv_usec * 1000) + dlay };
pthread_mutex_lock(&mutex);
pthread_cond_timedwait(&cond, &mutex, &timeout);
if(quit)
{
break;
}
pthread_mutex_unlock(&mutex);
std::cout << '\r' << "|/-\\" [x++ % 4] << std::flush;
}
pthread_mutex_unlock(&mutex);
return 0;
}