2
0
mirror of https://github.com/boostorg/thread.git synced 2026-02-08 23:22:13 +00:00
Files
thread/test/test_9303.cpp
Andrey Semashev ee609e8806 Cleanup header includes.
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.
2020-04-05 01:51:58 +03:00

180 lines
6.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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)
#define BOOST_THREAD_VERSION 4
#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <boost/function.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind/bind.hpp>
#include <boost/core/ref.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
#define EXAMPLE_1
#define EXAMPLE_2
#define EXAMPLE_3
#define EXAMPLE_4
#define EXAMPLE_5
#define EXAMPLE_6
#define EXAMPLE_7
#else
#define EXAMPLE_1
#define EXAMPLE_2
//#define EXAMPLE_3
//#define EXAMPLE_4
//#define EXAMPLE_5
//#define EXAMPLE_6
//#define EXAMPLE_7
#endif
// Test functions
int int_no_params()
{
return 42;
}
int int_with_params(int i)
{
return i;
}
std::string string_no_params()
{
return std::string("forty two");
}
std::string string_with_params(std::string& ans)
{
return ans;
}
int main(int /*argc*/, char ** /*argv[]*/)
{
std::string ans("forty two");
#if defined EXAMPLE_1
//! Compiles and produces correct result.
{
boost::packaged_task<int()> example(int_no_params);
boost::future<int> f = example.get_future();
boost::thread task(boost::move(example));
int answer = f.get();
std::cout << "Answer to life and whatnot, in English: " << answer << std::endl;
task.join();
}
#endif
#if defined EXAMPLE_2
//! Compiles and produces correct result.
{
boost::packaged_task<std::string()> example(string_no_params);
boost::future<std::string> f = example.get_future();
boost::thread task(boost::move(example));
std::string answer = f.get();
std::cout << "string_no_params: " << answer << std::endl;
task.join();
}
#endif
#if defined EXAMPLE_3
//! Doesn't compile in C++03.
//! error: variable "boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)> example" has initializer but incomplete type
{
boost::packaged_task<std::string(std::string&)> example(string_with_params);
boost::future<std::string> f = example.get_future();
example(ans);
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
}
#endif
#if defined EXAMPLE_4
//! Doesn't compile in C++11
// In file included from test_9303.cpp:10:
// In file included from ../../../boost/thread.hpp:13:
// In file included from ../../../boost/thread/thread.hpp:12:
// In file included from ../../../boost/thread/thread_only.hpp:22:
// ../../../boost/thread/detail/thread.hpp:76:15: error: no matching function for call to 'invoke'
// invoke(std::move(std::get<0>(fp)), std::move(std::get<Indices>(fp))...);
// ^~~~~~
{
boost::packaged_task<std::string(std::string&)> example(string_with_params);
boost::future<std::string> f = example.get_future();
boost::thread task(boost::move(example), boost::ref(ans));
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
task.join();
}
#endif
#if defined EXAMPLE_5
//! Doesn't compile in C++03, C++11 only.
//! error: extended initializer lists only available with -std=c++11 or -std=gnu++11 [-Werror]
{
boost::packaged_task<std::string(std::string&)> example
{ boost::bind(&string_with_params, ans) };
boost::future<std::string> f = example.get_future();
boost::thread task(boost::move(example), boost::ref(ans));
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
task.join();
}
#endif
#if defined EXAMPLE_6
//! Doesn't compile in C++03, C++11 only.
// packagedTestTest.cpp:94:43: error: invalid use of incomplete type class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>
// packagedTestTest.cpp:95:37: error: incomplete type task_t {aka boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>} used in nested name specifier
// boost/thread/future.hpp:1320:11: error: declaration of class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>
{
//typedef boost::packaged_task<std::string(std::string&)> task_t;
typedef boost::packaged_task<std::string()> task_t;
boost::shared_ptr<task_t> example = boost::make_shared<task_t>(boost::bind(&string_with_params, boost::ref(ans)));
boost::future<std::string> f = example->get_future();
boost::thread task(boost::bind(&task_t::operator(), example));
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
task.join();
}
#endif
#if defined EXAMPLE_7
//! Doesn't compile in C++03, C++11 only.
// packagedTestTest.cpp:94:43: error: invalid use of incomplete type class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>
// packagedTestTest.cpp:95:37: error: incomplete type task_t {aka boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>} used in nested name specifier
// boost/thread/future.hpp:1320:11: error: declaration of class boost::packaged_task<std::basic_string<char>(std::basic_string<char>&)>
{
boost::asio::io_service io_service;
boost::thread_group threads;
boost::asio::io_service::work work(io_service);
for (int i = 0; i < 3; ++i)
{
threads.create_thread(boost::bind(&boost::asio::io_service::run,
&io_service));
}
typedef boost::packaged_task<std::string()> task_t;
boost::shared_ptr<task_t> example = boost::make_shared<task_t>(boost::bind(&string_with_params, ans));
boost::future<std::string> f = example->get_future();
io_service.post(boost::bind(&task_t::operator(), example));
std::string answer = f.get();
std::cout << "string_with_params: " << answer << std::endl;
threads.join_all();
}
#endif
return 0;
}