Skip to content

Commit 175cdd3

Browse files
committed
Updated the examples that used the include directory that I removed.
1 parent 40bab67 commit 175cdd3

File tree

6 files changed

+32
-68
lines changed

6 files changed

+32
-68
lines changed

boost/network/utils/thread_group.hpp

+12-36
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,30 @@
99

1010
#include <thread>
1111
#include <mutex>
12-
#include <memory>
13-
#include <list>
14-
#include <algorithm>
12+
#include <vector>
1513

1614
namespace boost {
1715
namespace network {
1816
namespace utils {
1917
class thread_group {
20-
private:
21-
thread_group(thread_group const&);
22-
thread_group& operator=(thread_group const&);
23-
2418
public:
25-
thread_group() {}
26-
~thread_group() {}
19+
thread_group() = default;
20+
~thread_group() = default;
21+
thread_group(thread_group const&) = delete;
22+
thread_group& operator=(thread_group const&) = delete;
2723

2824
template <typename F>
29-
std::thread* create_thread(F threadfunc) {
30-
std::lock_guard<std::mutex> guard(m);
31-
std::unique_ptr<std::thread> new_thread(new std::thread(threadfunc));
32-
threads.push_back(std::move(new_thread));
33-
return threads.back().get();
34-
}
35-
36-
void add_thread(std::thread* thrd) {
37-
if (thrd) {
38-
std::lock_guard<std::mutex> guard(m);
39-
threads.push_back(std::unique_ptr<std::thread>(thrd));
40-
}
41-
}
42-
43-
void remove_thread(std::thread* thrd) {
25+
std::thread &create_thread(F threadfunc) {
4426
std::lock_guard<std::mutex> guard(m);
45-
auto const it = std::find_if(threads.begin(), threads.end(),
46-
[&thrd] (std::unique_ptr<std::thread> &arg) {
47-
return arg.get() == thrd;
48-
});
49-
if (it != threads.end()) {
50-
threads.erase(it);
51-
}
27+
threads.emplace_back(threadfunc);
28+
return threads.back();
5229
}
5330

5431
void join_all() {
5532
std::unique_lock<std::mutex> guard(m);
56-
57-
for (auto &thread : threads) {
58-
if (thread->joinable()) {
59-
thread->join();
33+
for (auto& thread : threads) {
34+
if (thread.joinable()) {
35+
thread.join();
6036
}
6137
}
6238
}
@@ -67,7 +43,7 @@ class thread_group {
6743
}
6844

6945
private:
70-
std::list<std::unique_ptr<std::thread>> threads;
46+
std::vector<std::thread> threads;
7147
mutable std::mutex m;
7248
};
7349

boost/network/utils/thread_pool.hpp

+16-28
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include <memory>
1111
#include <functional>
1212
#include <asio/io_service.hpp>
13-
#include <boost/function.hpp>
14-
#include <boost/network/tags.hpp>
1513
#include <boost/scope_exit.hpp>
1614
#include <boost/network/utils/thread_group.hpp>
1715

@@ -23,18 +21,18 @@ typedef std::shared_ptr<asio::io_service> io_service_ptr;
2321
typedef std::shared_ptr<utils::thread_group> worker_threads_ptr;
2422
typedef std::shared_ptr<asio::io_service::work> sentinel_ptr;
2523

26-
template <class Tag>
27-
struct basic_thread_pool {
28-
basic_thread_pool(basic_thread_pool const &) = delete;
29-
basic_thread_pool &operator=(basic_thread_pool) = delete;
30-
basic_thread_pool(basic_thread_pool&&) noexcept = default;
31-
basic_thread_pool &operator=(basic_thread_pool&&) = default;
24+
class thread_pool {
25+
public:
26+
thread_pool(thread_pool const &) = delete;
27+
thread_pool &operator=(thread_pool const &) = delete;
28+
thread_pool(thread_pool &&) noexcept = default;
29+
thread_pool &operator=(thread_pool &&) = default;
3230

33-
basic_thread_pool() : basic_thread_pool(1) {}
31+
thread_pool() : thread_pool(1) {}
3432

35-
explicit basic_thread_pool(std::size_t threads,
36-
io_service_ptr io_service = io_service_ptr(),
37-
worker_threads_ptr worker_threads = worker_threads_ptr())
33+
explicit thread_pool(std::size_t threads,
34+
io_service_ptr io_service = io_service_ptr(),
35+
worker_threads_ptr worker_threads = worker_threads_ptr())
3836
: threads_(threads),
3937
io_service_(std::move(io_service)),
4038
worker_threads_(std::move(worker_threads)),
@@ -46,7 +44,6 @@ struct basic_thread_pool {
4644
sentinel_.reset();
4745
io_service_.reset();
4846
if (worker_threads_.get()) {
49-
// worker_threads_->interrupt_all();
5047
worker_threads_->join_all();
5148
}
5249
worker_threads_.reset();
@@ -67,7 +64,7 @@ struct basic_thread_pool {
6764
}
6865

6966
for (std::size_t counter = 0; counter < threads_; ++counter) {
70-
worker_threads_->create_thread([=] () { io_service_->run(); });
67+
worker_threads_->create_thread([=]() { io_service_->run(); });
7168
}
7269

7370
commit = true;
@@ -77,18 +74,16 @@ struct basic_thread_pool {
7774

7875
void post(std::function<void()> f) { io_service_->post(f); }
7976

80-
~basic_thread_pool() throw() {
77+
~thread_pool() {
8178
sentinel_.reset();
8279
try {
8380
worker_threads_->join_all();
84-
}
85-
catch (...) {
86-
BOOST_ASSERT(false &&
87-
"A handler was not supposed to throw, but one did.");
81+
} catch (const std::exception &) {
82+
assert(!"A handler was not supposed to throw, but one did.");
8883
}
8984
}
9085

91-
void swap(basic_thread_pool &other) {
86+
void swap(thread_pool &other) {
9287
using std::swap;
9388
swap(other.threads_, threads_);
9489
swap(other.io_service_, io_service_);
@@ -101,16 +96,9 @@ struct basic_thread_pool {
10196
io_service_ptr io_service_;
10297
worker_threads_ptr worker_threads_;
10398
sentinel_ptr sentinel_;
104-
10599
};
106100

107-
template <class T>
108-
void swap(basic_thread_pool<T> &a, basic_thread_pool<T> &b) {
109-
a.swap(b);
110-
}
111-
112-
typedef basic_thread_pool<tags::default_> thread_pool;
113-
101+
void swap(thread_pool &a, thread_pool &b) { a.swap(b); }
114102
} // namespace utils
115103
} // namespace network
116104
} // namespace boost

libs/network/example/http/fileserver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <memory>
77
#include <thread>
8-
#include <boost/network/include/http/server.hpp>
8+
#include <boost/network/protocol/http/server.hpp>
99
#include <sys/mman.h>
1010
#include <sys/types.h>
1111
#include <sys/stat.h>

libs/network/example/http/hello_world_async_server_with_work_queue.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <chrono>
1313
#include <functional>
1414
#include <boost/network/utils/thread_group.hpp>
15-
#include <boost/network/include/http/server.hpp>
15+
#include <boost/network/protocol/http/server.hpp>
1616
#include <boost/network/uri.hpp>
1717
#include <asio.hpp>
1818
#include <iostream>

libs/network/example/http/ssl/ssl_server.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
#include <memory>
14-
#include <boost/network/include/http/server.hpp>
14+
#include <boost/network/protocol/http/server.hpp>
1515

1616
#include <asio.hpp>
1717
#include <asio/ssl.hpp>

libs/network/example/trivial_google.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <iostream>
88

9-
#include <boost/network/include/http/client.hpp>
9+
#include <boost/network/protocol/http/client.hpp>
1010

1111
namespace http = boost::network::http;
1212

0 commit comments

Comments
 (0)