mirror of
https://github.com/boostorg/thread.git
synced 2026-02-09 23:42:18 +00:00
1. Make inclusion of boost/bind/bind.hpp conditional in some cases, when the code actually conditionally uses boost::bind. Reduces compile-time overhead and fixes https://github.com/boostorg/thread/issues/307. 2. Remove some unnecessary uses of boost::ref. This allows to avoid including boost/core/ref.hpp in a few places, and avoids the associated template instantiation overhead in others. 3. Replace deprecated header includes with the more recent alternatives. For example: boost/detail/lightweight_test.hpp -> boost/core/lightweight_test.hpp, boost/ref.hpp -> boost/core/ref.hpp. 4. Replace some blanket includes with the more fine-grained ones. For example, boost/utility.hpp, boost/atomic.hpp. This reduces compile time overhead. 5. Add some missing includes, for example, boost/core/ref.hpp and boost/type_traits/is_same.hpp. 6. Replace uses of std::is_same with boost::is_same (with the corresponding included header) since the standard type_traits header presence and validity is not tested by the code. Using boost::is_same makes the code more portable.
150 lines
4.0 KiB
C++
150 lines
4.0 KiB
C++
// Copyright (C) 2014 Vicente Botet
|
|
//
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
// <boost/thread/future.hpp>
|
|
|
|
// class future<R>
|
|
|
|
// template<typename F>
|
|
// auto then(F&& func) -> future<decltype(func(*this))>;
|
|
|
|
#define BOOST_THREAD_VERSION 4
|
|
#define BOOST_THREAD_PROVIDES_EXECUTORS
|
|
//#define BOOST_THREAD_USES_LOG
|
|
#define BOOST_THREAD_USES_LOG_THREAD_ID
|
|
#include <boost/thread/detail/log.hpp>
|
|
|
|
#include <boost/thread/future.hpp>
|
|
#include <boost/thread/executors/basic_thread_pool.hpp>
|
|
#include <boost/thread/executor.hpp>
|
|
#include <boost/core/lightweight_test.hpp>
|
|
|
|
#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
|
|
|
|
#ifdef BOOST_MSVC
|
|
#pragma warning(disable: 4127) // conditional expression is constant
|
|
#endif
|
|
|
|
int p1()
|
|
{
|
|
BOOST_THREAD_LOG << "p1 < " << BOOST_THREAD_END_LOG;
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
|
|
BOOST_THREAD_LOG << "p1 >" << BOOST_THREAD_END_LOG;
|
|
return 1;
|
|
}
|
|
|
|
int p2(boost::shared_future<int> f)
|
|
{
|
|
BOOST_THREAD_LOG << "p2 <" << &f << BOOST_THREAD_END_LOG;
|
|
BOOST_TEST(f.valid());
|
|
int i = f.get();
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
|
|
BOOST_THREAD_LOG << "p2 <" << &f << BOOST_THREAD_END_LOG;
|
|
return 2 * i;
|
|
}
|
|
|
|
void p3(boost::shared_future<int> f)
|
|
{
|
|
BOOST_THREAD_LOG << "p3 <" << &f << BOOST_THREAD_END_LOG;
|
|
BOOST_TEST(f.valid());
|
|
int i = f.get();
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
|
|
BOOST_THREAD_LOG << "p3 <" << &f << " " << i << BOOST_THREAD_END_LOG;
|
|
return ;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
|
{
|
|
boost::basic_thread_pool ex(1);
|
|
boost::shared_future<int> f1 = boost::async(boost::launch::async, &p1).share();
|
|
BOOST_TEST(f1.valid());
|
|
boost::future<int> f2 = f1.then(ex, &p2);
|
|
BOOST_TEST(f2.valid());
|
|
try
|
|
{
|
|
BOOST_TEST(f2.get()==2);
|
|
}
|
|
catch (std::exception& ex)
|
|
{
|
|
BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
|
|
BOOST_TEST(false);
|
|
}
|
|
catch (...)
|
|
{
|
|
BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
|
|
BOOST_TEST(false);
|
|
}
|
|
}
|
|
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
|
{
|
|
boost::basic_thread_pool ex(1);
|
|
boost::shared_future<int> f1 = boost::async(boost::launch::async, &p1).share();
|
|
BOOST_TEST(f1.valid());
|
|
boost::future<void> f2 = f1.then(ex, &p3);
|
|
BOOST_TEST(f2.valid());
|
|
try
|
|
{
|
|
f2.wait();
|
|
}
|
|
catch (std::exception& ex)
|
|
{
|
|
BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
|
|
BOOST_TEST(false);
|
|
}
|
|
catch (...)
|
|
{
|
|
BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
|
|
BOOST_TEST(false);
|
|
}
|
|
}
|
|
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
|
{
|
|
boost::basic_thread_pool ex(1);
|
|
boost::future<int> f2 = boost::async(p1).share().then(ex, &p2);
|
|
BOOST_TEST(f2.get()==2);
|
|
}
|
|
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
|
{
|
|
boost::basic_thread_pool ex(1);
|
|
boost::shared_future<int> f1 = boost::async(p1).share();
|
|
boost::shared_future<int> f21 = f1.then(ex, &p2).share();
|
|
boost::future<int> f2= f21.then(ex, &p2);
|
|
BOOST_TEST(f2.get()==4);
|
|
}
|
|
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
|
{
|
|
boost::basic_thread_pool ex(1);
|
|
boost::shared_future<int> f1 = boost::async(p1).share();
|
|
boost::shared_future<int> f21 = f1.then(ex, &p2).share();
|
|
boost::future<int> f2= f21.then(&p2);
|
|
BOOST_TEST(f2.get()==4);
|
|
}
|
|
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
|
{
|
|
boost::basic_thread_pool ex(1);
|
|
boost::shared_future<int> f1 = boost::async(p1).share();
|
|
boost::future<int> f2= f1.then(ex, &p2).share().then(ex, &p2);
|
|
BOOST_TEST(f2.get()==4);
|
|
}
|
|
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
|
{
|
|
boost::basic_thread_pool ex(1);
|
|
boost::future<int> f2 = boost::async(p1).share().then(ex, &p2).share().then(ex, &p2);
|
|
BOOST_TEST(f2.get()==4);
|
|
}
|
|
|
|
return boost::report_errors();
|
|
}
|
|
|
|
#else
|
|
|
|
int main()
|
|
{
|
|
return 0;
|
|
}
|
|
#endif
|