-
Notifications
You must be signed in to change notification settings - Fork 1
/
avthread.cpp
50 lines (39 loc) · 849 Bytes
/
avthread.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
#include "avthread.h"
#include "avexception.h"
#include <iostream>
AVThread::AVThread():
thread(), attr(), running(false)
{
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
}
AVThread::~AVThread()
{
if (running) {
std::cerr << "AVThread: destructor called before joining" << std::endl;
}
}
bool AVThread::create()
{
if (running)
return false;
int ec = pthread_create(&thread, &attr, aCallback, this);
if (ec != 0)
return false;
return true;
}
bool AVThread::join()
{
int ec = pthread_join(thread, 0);
if (ec != 0)
return false;
return true;
}
void *AVThread::aCallback(void *data)
{
AVThread *me = static_cast<AVThread *>(data);
me->running = true;
me->run();
me->running = false;
pthread_exit(0);
}