-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.hpp
More file actions
54 lines (40 loc) · 857 Bytes
/
agent.hpp
File metadata and controls
54 lines (40 loc) · 857 Bytes
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
#include <exception>
#include <pthread.h>
namespace CppAgent {
class BaseAgent {
public:
virtual void main () = 0;
};
class AgentThread {
pthread_t _thread;
BaseAgent &_agent;
public:
AgentThread (BaseAgent &agent) : _agent (agent) {
void *start_main (void *);
//launch thread
pthread_create (&_thread, NULL, start_main, &agent);
}
~AgentThread () {
//wait for thread to terminate
std::exception *err;
pthread_join (_thread, reinterpret_cast <void **> (&err));
//propagate errors that occur in thread
if (err != NULL) {
throw *err;
}
}
};
void *start_main (void *agent) {
try {
static_cast <BaseAgent *> (agent)->main ();
}
catch (std::exception &err) {
pthread_exit (&err);
}
catch (...) {
std::exception err;
pthread_exit (&err);
}
pthread_exit (NULL);
}
}