-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool.cpp
More file actions
112 lines (95 loc) · 2.81 KB
/
thread_pool.cpp
File metadata and controls
112 lines (95 loc) · 2.81 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2012-2013 Vicente Botet
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "simple_stopwatch.hpp"
#include <boost/config.hpp>
#if !defined BOOST_NO_CXX11_DECLTYPE
# define BOOST_RESULT_OF_USE_DECLTYPE
#endif
#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_PROVIDES_EXECUTORS
#define BOOST_THREAD_QUEUE_DEPRECATE_OLD
#define BOOST_THREAD_USES_LOG
#define BOOST_THREAD_USES_LOG_THREAD_ID
#include <boost/thread/detail/log.hpp>
#include <boost/thread/executor.hpp>
#include <boost/thread/future.hpp>
#include <boost/thread/thread_pool.hpp>
#include <exception>
#include <iostream>
#ifdef BOOST_MSVC
# pragma warning(disable : 4127) // conditional expression is constant
#endif
int runOutOfMemroy()
{
BOOST_THREAD_LOG << " " << BOOST_CURRENT_FUNCTION << BOOST_THREAD_END_LOG;
#if 0
while (true) {
new int[1000000000ul]; // throwing overload
}
return 0;
#else
throw std::bad_alloc();
#endif
}
void p1()
{
BOOST_THREAD_LOG << " " << BOOST_CURRENT_FUNCTION << BOOST_THREAD_END_LOG;
}
void p2()
{
BOOST_THREAD_LOG << " " << BOOST_CURRENT_FUNCTION << BOOST_THREAD_END_LOG;
}
void submit_some(boost::basic_thread_pool& tp)
{
tp.submit(&p1);
tp.submit(&p2);
tp.submit(&p1);
tp.submit(&p2);
tp.submit(&p1);
tp.submit(&p2);
tp.submit(&p1);
tp.submit(&p2);
tp.submit(&p1);
tp.submit(&p2);
boost::this_thread::sleep_for(ms(10));
}
int main()
{
BOOST_THREAD_LOG << " " << BOOST_CURRENT_FUNCTION << BOOST_THREAD_END_LOG;
{
try {
StopwatchReporter sw;
boost::basic_thread_pool tp(2);
submit_some(tp);
boost::future<int> t1 = boost::async(tp, &runOutOfMemroy);
boost::this_thread::sleep_for(ms(10));
std::cout << "\n t1 = " << t1.get() << std::endl;
} catch (std::exception& ex) {
std::cerr << "\n Exception " << ex.what() << std::endl;
// OK; expected CK
}
try {
StopwatchReporter sw;
#ifdef BOOST_THREAD_PROVIDES_EXECUTORS
//==========================
boost::executor_adaptor<boost::basic_thread_pool> ea(1);
ea.submit(&p1);
ea.submit(&p2);
boost::future<int> t1 = boost::async(ea, &runOutOfMemroy);
boost::this_thread::sleep_for(ms(10));
std::cout << "\n t1 = " << t1.get() << std::endl;
//==========================
#else
runOutOfMemroy();
#endif
} catch (std::exception& ex) {
std::cerr << "\n Exception " << ex.what() << std::endl;
// OK; expected CK
}
}
BOOST_THREAD_LOG << " " << BOOST_CURRENT_FUNCTION << BOOST_THREAD_END_LOG;
return 0;
}