-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy paththread.exception.cpp
More file actions
54 lines (44 loc) · 1.04 KB
/
thread.exception.cpp
File metadata and controls
54 lines (44 loc) · 1.04 KB
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 <boost/exception_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>
struct file_read_error: virtual boost::exception {};
typedef boost::error_info<struct tag_md_message, std::string> err_message;
using namespace std;
using namespace boost;
void do_work()
{
this_thread::sleep(posix_time::seconds(2));
#if 1
throw boost::enable_current_exception(file_read_error())
<< err_message("leech");
#endif
}
void worker_thread(boost::exception_ptr& error)
{
try
{
do_work();
error = boost::exception_ptr();
this_thread::sleep(posix_time::seconds(5));
}
catch (...)
{
error = boost::current_exception();
}
}
int main(int argc, char* argv[])
{
boost::exception_ptr error;
boost::thread t(boost::bind(worker_thread, boost::ref(error)));
t.join();
this_thread::sleep(posix_time::seconds(5));
if (error)
{
if (string const* mi = boost::get_error_info<err_message>(*error))
cout << *mi;
cout << boost::diagnostic_information(*error);
}
return 0;
}