Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 63d9ae2

Browse filesBrowse files
committed
Thread: Added a polymorphic executor and an executor_adaptor.
[SVN r86161]
1 parent 08aa0d2 commit 63d9ae2
Copy full SHA for 63d9ae2

File tree

Expand file treeCollapse file tree

4 files changed

+361
-17
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+361
-17
lines changed
Open diff view settings
Collapse file

‎example/executor.cpp‎

Copy file name to clipboard
+149Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright (C) 2012-2013 Vicente Botet
2+
//
3+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#define BOOST_THREAD_VERSION 4
7+
#define BOOST_THREAD_USES_LOG
8+
#define BOOST_THREAD_USES_LOG_THREAD_ID
9+
10+
#include <boost/thread/detail/log.hpp>
11+
#include <boost/thread/thread_pool.hpp>
12+
#include <boost/thread/executor.hpp>
13+
#include <boost/assert.hpp>
14+
#include <string>
15+
16+
void p1()
17+
{
18+
BOOST_THREAD_LOG
19+
<< boost::this_thread::get_id() << " P1" << BOOST_THREAD_END_LOG;
20+
}
21+
22+
void p2()
23+
{
24+
BOOST_THREAD_LOG
25+
<< boost::this_thread::get_id() << " P2" << BOOST_THREAD_END_LOG;
26+
}
27+
28+
void push(boost::container::deque<boost::detail::function_wrapper> &data_, BOOST_THREAD_RV_REF(boost::detail::function_wrapper) closure)
29+
{
30+
try
31+
{
32+
BOOST_THREAD_LOG
33+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
34+
boost::detail::function_wrapper v;
35+
BOOST_THREAD_LOG
36+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
37+
//v = boost::move(closure);
38+
//v = boost::forward<boost::detail::function_wrapper>(closure);
39+
BOOST_THREAD_LOG
40+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
41+
42+
data_.push_back(boost::move(closure));
43+
BOOST_THREAD_LOG
44+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
45+
46+
//data_.push_back(boost::forward<boost::detail::function_wrapper>(closure));
47+
BOOST_THREAD_LOG
48+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
49+
50+
}
51+
catch (std::exception& ex)
52+
{
53+
BOOST_THREAD_LOG
54+
<< "ERRORRRRR " << ex.what() << "" << BOOST_THREAD_END_LOG;
55+
}
56+
catch (...)
57+
{
58+
BOOST_THREAD_LOG
59+
<< " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
60+
}
61+
}
62+
63+
template <typename Closure>
64+
void submit(boost::container::deque<boost::detail::function_wrapper> &data_, BOOST_THREAD_FWD_REF(Closure) closure)
65+
{
66+
BOOST_THREAD_LOG
67+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
68+
//work w =boost::move(closure);
69+
//work_queue.push(boost::move(w));
70+
//push(data_, boost::detail::function_wrapper(boost::forward<Closure>(closure)));
71+
boost::detail::function_wrapper v =boost::forward<Closure>(closure);
72+
BOOST_THREAD_LOG
73+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
74+
push(data_, boost::move(v));
75+
76+
BOOST_THREAD_LOG
77+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
78+
}
79+
80+
int main()
81+
{
82+
BOOST_THREAD_LOG
83+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
84+
#if 0
85+
{
86+
try
87+
{
88+
boost::detail::function_wrapper f(&p1);
89+
90+
boost::container::deque<boost::detail::function_wrapper> data_;
91+
data_.push_back(boost::move(f));
92+
data_.push_back(boost::detail::function_wrapper(&p1));
93+
submit(data_, &p1);
94+
}
95+
catch (std::exception& ex)
96+
{
97+
BOOST_THREAD_LOG
98+
<< "ERRORRRRR " << ex.what() << "" << BOOST_THREAD_END_LOG;
99+
}
100+
catch (...)
101+
{
102+
BOOST_THREAD_LOG
103+
<< " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
104+
}
105+
106+
typedef boost::container::vector<boost::thread> thread_vector;
107+
thread_vector threads;
108+
109+
}
110+
#endif
111+
#if 1
112+
{
113+
try
114+
{
115+
boost::executor_adaptor<boost::thread_pool> ea;
116+
boost::executor &tp=ea;
117+
BOOST_THREAD_LOG
118+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
119+
tp.submit(&p1);
120+
BOOST_THREAD_LOG
121+
<< boost::this_thread::get_id() << " <MAIN" << BOOST_THREAD_END_LOG;
122+
tp.submit(&p2);
123+
tp.submit(&p1);
124+
tp.submit(&p2);
125+
tp.submit(&p1);
126+
tp.submit(&p2);
127+
tp.submit(&p1);
128+
tp.submit(&p2);
129+
tp.submit(&p1);
130+
tp.submit(&p2);
131+
}
132+
catch (std::exception& ex)
133+
{
134+
BOOST_THREAD_LOG
135+
<< "ERRORRRRR " << ex.what() << "" << BOOST_THREAD_END_LOG;
136+
return 1;
137+
}
138+
catch (...)
139+
{
140+
BOOST_THREAD_LOG
141+
<< " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
142+
return 2;
143+
}
144+
}
145+
#endif
146+
BOOST_THREAD_LOG
147+
<< boost::this_thread::get_id() << "MAIN>" << BOOST_THREAD_END_LOG;
148+
return 0;
149+
}
Collapse file

‎include/boost/thread/executor.hpp‎

Copy file name to clipboard
+175Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Copyright (C) 2013 Vicente J. Botet Escriba
2+
//
3+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5+
//
6+
// 2013/09 Vicente J. Botet Escriba
7+
// Adapt to boost from CCIA C++11 implementation
8+
9+
#ifndef BOOST_THREAD_EXECUTOR_HPP
10+
#define BOOST_THREAD_EXECUTOR_HPP
11+
12+
#include <boost/thread/detail/config.hpp>
13+
14+
15+
#include <boost/thread/detail/delete.hpp>
16+
#include <boost/thread/detail/move.hpp>
17+
#include <boost/thread/detail/function_wrapper.hpp>
18+
19+
20+
#include <boost/config/abi_prefix.hpp>
21+
22+
namespace boost
23+
{
24+
25+
class executor
26+
{
27+
public:
28+
/// type-erasure to store the works to do
29+
typedef detail::function_wrapper work;
30+
31+
/// executor is not copyable.
32+
BOOST_THREAD_NO_COPYABLE(executor)
33+
executor() {}
34+
35+
/**
36+
* \b Effects: Destroys the executor.
37+
*
38+
* \b Synchronization: The completion of all the closures happen before the completion of the executor destructor.
39+
*/
40+
virtual ~executor() {};
41+
42+
/**
43+
* \b Effects: close the \c executor for submissions.
44+
* The worker threads will work until there is no more closures to run.
45+
*/
46+
virtual void close() = 0;
47+
48+
/**
49+
* \b Returns: whether the pool is closed for submissions.
50+
*/
51+
virtual bool closed() = 0;
52+
53+
/**
54+
* \b Effects: The specified closure will be scheduled for execution at some point in the future.
55+
* If invoked closure throws an exception the executor will call std::terminate, as is the case with threads.
56+
*
57+
* \b Synchronization: completion of closure on a particular thread happens before destruction of thread's thread local variables.
58+
*
59+
* \b Throws: \c sync_queue_is_closed if the thread pool is closed.
60+
* Whatever exception that can be throw while storing the closure.
61+
*/
62+
virtual void submit(BOOST_THREAD_RV_REF(work) closure) = 0;
63+
64+
/**
65+
* \b Requires: \c Closure is a model of Callable(void()) and a model of CopyConstructible/MoveConstructible.
66+
*
67+
* \b Effects: The specified closure will be scheduled for execution at some point in the future.
68+
* If invoked closure throws an exception the thread pool will call std::terminate, as is the case with threads.
69+
*
70+
* \b Synchronization: completion of closure on a particular thread happens before destruction of thread's thread local variables.
71+
*
72+
* \b Throws: \c sync_queue_is_closed if the thread pool is closed.
73+
* Whatever exception that can be throw while storing the closure.
74+
*/
75+
//#if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
76+
template <typename Closure>
77+
void submit(Closure const& closure)
78+
{
79+
work w ((closure));
80+
submit(boost::move(w));
81+
}
82+
//#endif
83+
84+
template <typename Closure>
85+
void submit(BOOST_THREAD_RV_REF(Closure) closure)
86+
{
87+
work w = boost::move(closure);
88+
submit(boost::move(w));
89+
}
90+
91+
/**
92+
* Effects: try to execute one task.
93+
* Returns: whether a task has been executed.
94+
* Throws: whatever the current task constructor throws or the task() throws.
95+
*/
96+
virtual bool try_executing_one() = 0;
97+
98+
/**
99+
* \b Requires: This must be called from an scheduled task.
100+
*
101+
* \b Effects: reschedule functions until pred()
102+
*/
103+
template <typename Pred>
104+
bool reschedule_until(Pred const& pred)
105+
{
106+
do {
107+
//schedule_one_or_yield();
108+
if ( ! try_executing_one())
109+
{
110+
return false;
111+
}
112+
} while (! pred());
113+
return true;
114+
}
115+
};
116+
template <typename Executor>
117+
class executor_adaptor : public executor
118+
{
119+
Executor ex;
120+
public:
121+
/// type-erasure to store the works to do
122+
typedef detail::function_wrapper work;
123+
124+
/// executor is not copyable.
125+
BOOST_THREAD_NO_COPYABLE(executor_adaptor)
126+
127+
/**
128+
* executor_adaptor constructor
129+
*/
130+
executor_adaptor() : ex() {}
131+
132+
/**
133+
* executor_adaptor constructor
134+
*/
135+
#if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
136+
template <typename ...Args>
137+
executor_adaptor(BOOST_THREAD_RV_REF(Args) ... args) : ex(boost::forward<Args>(args)...) {}
138+
#endif
139+
/**
140+
* \b Effects: close the \c executor for submissions.
141+
* The worker threads will work until there is no more closures to run.
142+
*/
143+
void close() { ex.close(); }
144+
145+
/**
146+
* \b Returns: whether the pool is closed for submissions.
147+
*/
148+
bool closed() { return ex.closed(); }
149+
150+
/**
151+
* \b Effects: The specified closure will be scheduled for execution at some point in the future.
152+
* If invoked closure throws an exception the executor will call std::terminate, as is the case with threads.
153+
*
154+
* \b Synchronization: completion of closure on a particular thread happens before destruction of thread's thread local variables.
155+
*
156+
* \b Throws: \c sync_queue_is_closed if the thread pool is closed.
157+
* Whatever exception that can be throw while storing the closure.
158+
*/
159+
void submit(BOOST_THREAD_RV_REF(work) closure) { return ex.submit(boost::move(closure)); }
160+
161+
162+
/**
163+
* Effects: try to execute one task.
164+
* Returns: whether a task has been executed.
165+
* Throws: whatever the current task constructor throws or the task() throws.
166+
*/
167+
bool try_executing_one() { return ex.try_executing_one(); }
168+
169+
};
170+
171+
}
172+
173+
#include <boost/config/abi_suffix.hpp>
174+
175+
#endif

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.