Skip to content

Commit

Permalink
Convert uses of boost::thread to std::thread
Browse files Browse the repository at this point in the history
Also covers uses of boost::this_thread.

Turns out std::thread calls std::terminate if it's destroyed while still joinable.
thread::detach needs to be called or else the program will crash.

There's also no standard library equivalent of boost::thread::timed_join so I just
replaced it with the is_worker_running_ variable.
  • Loading branch information
Vultraz committed Oct 17, 2018
1 parent ae5c5c4 commit 519447b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/events.cpp
Expand Up @@ -31,11 +31,11 @@
#include <iterator>
#include <utility>
#include <vector>
#include <thread>

#include <SDL.h>

#include <boost/range/adaptor/reversed.hpp>
#include <boost/thread.hpp>

#define ERR_GEN LOG_STREAM(err, lg::general)

Expand Down Expand Up @@ -420,11 +420,11 @@ static bool remove_on_resize(const SDL_Event& a)
}

// TODO: I'm uncertain if this is always safe to call at static init; maybe set in main() instead?
static const boost::thread::id main_thread = boost::this_thread::get_id();
static const std::thread::id main_thread = std::this_thread::get_id();

void pump()
{
if(boost::this_thread::get_id() != main_thread) {
if(std::this_thread::get_id() != main_thread) {
// Can only call this on the main thread!
return;
}
Expand Down Expand Up @@ -756,7 +756,7 @@ void peek_for_resize()

void call_in_main_thread(const std::function<void(void)>& f)
{
if(boost::this_thread::get_id() == main_thread) {
if(std::this_thread::get_id() == main_thread) {
// nothing special to do if called from the main thread.
f();
return;
Expand Down
7 changes: 3 additions & 4 deletions src/gui/dialogs/loading_screen.cpp
Expand Up @@ -32,8 +32,6 @@
#include "utils/functional.hpp"
#include "video.hpp"

#include <boost/thread.hpp>

#include <cstdlib>

#if defined(_MSC_VER) && _MSC_VER <= 1800
Expand Down Expand Up @@ -109,7 +107,7 @@ loading_screen::loading_screen(std::function<void()> f)
void loading_screen::pre_show(window& window)
{
if(work_) {
worker_.reset(new boost::thread([this]() {
worker_.reset(new std::thread([this]() {
is_worker_running_ = true;

try {
Expand Down Expand Up @@ -154,12 +152,13 @@ loading_screen* loading_screen::current_load = nullptr;

void loading_screen::timer_callback(window& window)
{
if(!work_ || !worker_ || worker_->timed_join(boost::posix_time::milliseconds(0))) {
if(!work_ || !worker_ || !is_worker_running_) {
if(exception_) {
clear_timer();
std::rethrow_exception(exception_);
}

worker_->detach();
window.close();
}

Expand Down
7 changes: 2 additions & 5 deletions src/gui/dialogs/loading_screen.hpp
Expand Up @@ -19,11 +19,8 @@
#include <map>
#include <vector>
#include <atomic>
#include <thread>

namespace boost
{
class thread;
}
namespace cursor
{
struct setter;
Expand Down Expand Up @@ -86,7 +83,7 @@ class loading_screen : public modal_dialog
std::size_t timer_id_;
int animation_counter_;
std::function<void()> work_;
std::unique_ptr<boost::thread> worker_;
std::unique_ptr<std::thread> worker_;
std::unique_ptr<cursor::setter> cursor_setter_;
std::exception_ptr exception_;
void clear_timer();
Expand Down
5 changes: 2 additions & 3 deletions src/wesnothd_connection.cpp
Expand Up @@ -22,8 +22,6 @@

#include <SDL_timer.h>

#include <boost/thread.hpp>

#include <cstdint>
#include <deque>

Expand Down Expand Up @@ -83,6 +81,7 @@ wesnothd_connection::wesnothd_connection(const std::string& host, const std::str

wesnothd_connection::~wesnothd_connection()
{
worker_thread_->detach();
MPTEST_LOG;

// Stop the io_service and wait for the worker thread to terminate.
Expand Down Expand Up @@ -155,7 +154,7 @@ void wesnothd_connection::handle_handshake(const error_code& ec)
handshake_finished_ = true;
recv();

worker_thread_.reset(new boost::thread([this]() {
worker_thread_.reset(new std::thread([this]() {
io_service_.run();
LOG_NW << "wesnothd_connection::io_service::run() returned\n";
}));
Expand Down
8 changes: 2 additions & 6 deletions src/wesnothd_connection.hpp
Expand Up @@ -37,13 +37,9 @@

#include <deque>
#include <list>
#include <thread>
#include <mutex>

namespace boost
{
class thread;
}

class config;
enum class loading_stage;

Expand Down Expand Up @@ -129,7 +125,7 @@ class wesnothd_connection
}

private:
std::unique_ptr<boost::thread> worker_thread_;
std::unique_ptr<std::thread> worker_thread_;

boost::asio::io_service io_service_;

Expand Down

0 comments on commit 519447b

Please sign in to comment.