Convert uses of boost::thread to std::thread

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:🧵:timed_join so I just
replaced it with the is_worker_running_ variable.
This commit is contained in:
Charles Dang 2018-04-13 01:08:41 +11:00
parent 19d9e150e1
commit 2edcbdced8
5 changed files with 13 additions and 22 deletions

View file

@ -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)
@ -415,11 +415,11 @@ void finalize()
}
// 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 run_event_loop()
{
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;
}
@ -746,7 +746,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;

View file

@ -32,8 +32,6 @@
#include "utils/functional.hpp"
#include "video.hpp"
#include <boost/thread.hpp>
#include <cstdlib>
#if defined(_MSC_VER) && _MSC_VER <= 1800
@ -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 {
@ -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();
}

View file

@ -19,11 +19,8 @@
#include <map>
#include <vector>
#include <atomic>
#include <thread>
namespace boost
{
class thread;
}
namespace cursor
{
struct setter;
@ -86,7 +83,7 @@ private:
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();

View file

@ -21,8 +21,6 @@
#include <SDL_timer.h>
#include <boost/thread.hpp>
#include <cstdint>
#include <deque>
@ -82,6 +80,7 @@ wesnothd_connection::wesnothd_connection(const std::string& host, const std::str
wesnothd_connection::~wesnothd_connection()
{
worker_thread_->detach();
MPTEST_LOG;
}
@ -150,7 +149,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]() {
// worker thread
std::shared_ptr<wesnothd_connection> this_ptr = this->shared_from_this();

View file

@ -41,13 +41,9 @@
#include <deque>
#include <list>
#include <thread>
#include <mutex>
namespace boost
{
class thread;
}
class config;
class wesnothd_connection_ptr;
enum class loading_stage;
@ -139,7 +135,7 @@ public:
}
private:
std::unique_ptr<boost::thread> worker_thread_;
std::unique_ptr<std::thread> worker_thread_;
boost::asio::io_service io_service_;