summaryrefslogtreecommitdiff
path: root/libs/thread/test/test_9303.cpp
blob: a49758a1a2053d529e05547b08ce5e16e79adc9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#define BOOST_THREAD_VERSION 4
    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    #include <boost/function.hpp>
    #include <boost/make_shared.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/bind.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;
    }