use boost::async instead of std::async as ~boost::async is nonblocking

This commit is contained in:
Andrzej Rybczak
2016-12-21 21:43:36 +01:00
parent 50ec522726
commit fd59e2af96
5 changed files with 25 additions and 15 deletions

View File

@@ -143,6 +143,17 @@ PKG_CHECK_MODULES([ICU], [icu-uc], [
) )
], [[]]) ], [[]])
dnl =============================
dnl = checking for boost.thread =
dnl =============================
AC_DEFINE([BOOST_THREAD_VERSION], [3], [require boost.thread v3])
AC_CHECK_HEADERS([boost/thread.hpp], ,
AC_MSG_ERROR([boost/thread.hpp is missing or your boost version is too old (boost.thread v3 is required)])
)
AC_CHECK_LIB(boost_thread$BOOST_LIB_SUFFIX, main, LIBS="$LIBS -lboost_thread$BOOST_LIB_SUFFIX",
AC_MSG_ERROR([no boost.thread library found])
)
dnl ================================ dnl ================================
dnl = checking for various headers = dnl = checking for various headers =
dnl ================================ dnl ================================

View File

@@ -52,11 +52,8 @@ std::wstring Lastfm::title()
void Lastfm::update() void Lastfm::update()
{ {
if (m_worker.valid() if (m_worker.valid() && m_worker.is_ready())
&& m_worker.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
getResult(); getResult();
}
} }
void Lastfm::switchTo() void Lastfm::switchTo()
@@ -85,5 +82,5 @@ void Lastfm::getResult()
w.flush(); w.flush();
w.refresh(); w.refresh();
// reset m_worker so it's no longer valid // reset m_worker so it's no longer valid
m_worker = std::future<LastFm::Service::Result>(); m_worker = boost::BOOST_THREAD_FUTURE<LastFm::Service::Result>();
} }

View File

@@ -23,7 +23,7 @@
#include "config.h" #include "config.h"
#include <future> #include <boost/thread/future.hpp>
#include <memory> #include <memory>
#include "interfaces.h" #include "interfaces.h"
@@ -55,8 +55,9 @@ struct Lastfm: Screen<NC::Scrollpad>, Tabbable
return; return;
m_service = std::shared_ptr<ServiceT>(service); m_service = std::shared_ptr<ServiceT>(service);
m_worker = std::async(std::launch::async, m_worker = boost::async(
std::bind(&LastFm::Service::fetch, m_service)); boost::launch::async,
std::bind(&LastFm::Service::fetch, m_service));
w.clear(); w.clear();
w << "Fetching information..."; w << "Fetching information...";
@@ -70,7 +71,7 @@ private:
std::wstring m_title; std::wstring m_title;
std::shared_ptr<LastFm::Service> m_service; std::shared_ptr<LastFm::Service> m_service;
std::future<LastFm::Service::Result> m_worker; boost::BOOST_THREAD_FUTURE<LastFm::Service::Result> m_worker;
}; };
extern Lastfm *myLastfm; extern Lastfm *myLastfm;

View File

@@ -25,6 +25,7 @@
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <thread>
#include "browser.h" #include "browser.h"
#include "charset.h" #include "charset.h"
@@ -213,7 +214,7 @@ void Lyrics::update()
m_refresh_window = true; m_refresh_window = true;
} }
if (m_worker.wait_for(std::chrono::seconds(0)) == std::future_status::ready) if (m_worker.is_ready())
{ {
auto lyrics = m_worker.get(); auto lyrics = m_worker.get();
if (lyrics) if (lyrics)
@@ -279,8 +280,8 @@ void Lyrics::fetch(const MPD::Song &s)
else else
{ {
m_shared_buffer = std::make_shared<Shared<NC::Buffer>>(); m_shared_buffer = std::make_shared<Shared<NC::Buffer>>();
m_worker = std::async( m_worker = boost::async(
std::launch::async, boost::launch::async,
std::bind(downloadLyrics, m_song, m_shared_buffer, m_fetcher)); std::bind(downloadLyrics, m_song, m_shared_buffer, m_fetcher));
} }
} }
@@ -422,5 +423,5 @@ boost::optional<std::string> Lyrics::tryTakeConsumerMessage()
void Lyrics::clearWorker() void Lyrics::clearWorker()
{ {
m_shared_buffer.reset(); m_shared_buffer.reset();
m_worker = std::future<boost::optional<std::string>>(); m_worker = boost::BOOST_THREAD_FUTURE<boost::optional<std::string>>();
} }

View File

@@ -22,7 +22,7 @@
#define NCMPCPP_LYRICS_H #define NCMPCPP_LYRICS_H
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <future> #include <boost/thread/future.hpp>
#include <memory> #include <memory>
#include <queue> #include <queue>
@@ -96,7 +96,7 @@ private:
MPD::Song m_song; MPD::Song m_song;
LyricsFetcher *m_fetcher; LyricsFetcher *m_fetcher;
std::future<boost::optional<std::string>> m_worker; boost::BOOST_THREAD_FUTURE<boost::optional<std::string>> m_worker;
Shared<ConsumerState> m_consumer_state; Shared<ConsumerState> m_consumer_state;
}; };