Skip to content
Prev Previous commit
Next Next commit
Updated the examples that used the include directory that I removed.
  • Loading branch information
glynos committed Apr 9, 2016
commit 175cdd3782185eade78b540cb767c5c2ccadae4f
48 changes: 12 additions & 36 deletions boost/network/utils/thread_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,30 @@

#include <thread>
#include <mutex>
#include <memory>
#include <list>
#include <algorithm>
#include <vector>

namespace boost {
namespace network {
namespace utils {
class thread_group {
private:
thread_group(thread_group const&);
thread_group& operator=(thread_group const&);

public:
thread_group() {}
~thread_group() {}
thread_group() = default;
~thread_group() = default;
thread_group(thread_group const&) = delete;
thread_group& operator=(thread_group const&) = delete;

template <typename F>
std::thread* create_thread(F threadfunc) {
std::lock_guard<std::mutex> guard(m);
std::unique_ptr<std::thread> new_thread(new std::thread(threadfunc));
threads.push_back(std::move(new_thread));
return threads.back().get();
}

void add_thread(std::thread* thrd) {
if (thrd) {
std::lock_guard<std::mutex> guard(m);
threads.push_back(std::unique_ptr<std::thread>(thrd));
}
}

void remove_thread(std::thread* thrd) {
std::thread &create_thread(F threadfunc) {
std::lock_guard<std::mutex> guard(m);
auto const it = std::find_if(threads.begin(), threads.end(),
[&thrd] (std::unique_ptr<std::thread> &arg) {
return arg.get() == thrd;
});
if (it != threads.end()) {
threads.erase(it);
}
threads.emplace_back(threadfunc);
return threads.back();
}

void join_all() {
std::unique_lock<std::mutex> guard(m);

for (auto &thread : threads) {
if (thread->joinable()) {
thread->join();
for (auto& thread : threads) {
if (thread.joinable()) {
thread.join();
}
}
}
Expand All @@ -67,7 +43,7 @@ class thread_group {
}

private:
std::list<std::unique_ptr<std::thread>> threads;
std::vector<std::thread> threads;
mutable std::mutex m;
};

Expand Down
44 changes: 16 additions & 28 deletions boost/network/utils/thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <memory>
#include <functional>
#include <asio/io_service.hpp>
#include <boost/function.hpp>
#include <boost/network/tags.hpp>
#include <boost/scope_exit.hpp>
#include <boost/network/utils/thread_group.hpp>

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

template <class Tag>
struct basic_thread_pool {
basic_thread_pool(basic_thread_pool const &) = delete;
basic_thread_pool &operator=(basic_thread_pool) = delete;
basic_thread_pool(basic_thread_pool&&) noexcept = default;
basic_thread_pool &operator=(basic_thread_pool&&) = default;
class thread_pool {
public:
thread_pool(thread_pool const &) = delete;
thread_pool &operator=(thread_pool const &) = delete;
thread_pool(thread_pool &&) noexcept = default;
thread_pool &operator=(thread_pool &&) = default;

basic_thread_pool() : basic_thread_pool(1) {}
thread_pool() : thread_pool(1) {}

explicit basic_thread_pool(std::size_t threads,
io_service_ptr io_service = io_service_ptr(),
worker_threads_ptr worker_threads = worker_threads_ptr())
explicit thread_pool(std::size_t threads,
io_service_ptr io_service = io_service_ptr(),
worker_threads_ptr worker_threads = worker_threads_ptr())
: threads_(threads),
io_service_(std::move(io_service)),
worker_threads_(std::move(worker_threads)),
Expand All @@ -46,7 +44,6 @@ struct basic_thread_pool {
sentinel_.reset();
io_service_.reset();
if (worker_threads_.get()) {
// worker_threads_->interrupt_all();
worker_threads_->join_all();
}
worker_threads_.reset();
Expand All @@ -67,7 +64,7 @@ struct basic_thread_pool {
}

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

commit = true;
Expand All @@ -77,18 +74,16 @@ struct basic_thread_pool {

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

~basic_thread_pool() throw() {
~thread_pool() {
sentinel_.reset();
try {
worker_threads_->join_all();
}
catch (...) {
BOOST_ASSERT(false &&
"A handler was not supposed to throw, but one did.");
} catch (const std::exception &) {
assert(!"A handler was not supposed to throw, but one did.");
}
}

void swap(basic_thread_pool &other) {
void swap(thread_pool &other) {
using std::swap;
swap(other.threads_, threads_);
swap(other.io_service_, io_service_);
Expand All @@ -101,16 +96,9 @@ struct basic_thread_pool {
io_service_ptr io_service_;
worker_threads_ptr worker_threads_;
sentinel_ptr sentinel_;

};

template <class T>
void swap(basic_thread_pool<T> &a, basic_thread_pool<T> &b) {
a.swap(b);
}

typedef basic_thread_pool<tags::default_> thread_pool;

void swap(thread_pool &a, thread_pool &b) { a.swap(b); }
} // namespace utils
} // namespace network
} // namespace boost
Expand Down
2 changes: 1 addition & 1 deletion libs/network/example/http/fileserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <memory>
#include <thread>
#include <boost/network/include/http/server.hpp>
#include <boost/network/protocol/http/server.hpp>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <chrono>
#include <functional>
#include <boost/network/utils/thread_group.hpp>
#include <boost/network/include/http/server.hpp>
#include <boost/network/protocol/http/server.hpp>
#include <boost/network/uri.hpp>
#include <asio.hpp>
#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion libs/network/example/http/ssl/ssl_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

#include <memory>
#include <boost/network/include/http/server.hpp>
#include <boost/network/protocol/http/server.hpp>

#include <asio.hpp>
#include <asio/ssl.hpp>
Expand Down
2 changes: 1 addition & 1 deletion libs/network/example/trivial_google.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <iostream>

#include <boost/network/include/http/client.hpp>
#include <boost/network/protocol/http/client.hpp>

namespace http = boost::network::http;

Expand Down