summaryrefslogtreecommitdiff
path: root/libs/thread/test/threads/thread/constr/FArgs_pass.cpp
blob: 6a573fe3ea16da395a5f0d7a8e74782a4033309f (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
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Copyright (C) 2011 Vicente J. Botet Escriba
//
//  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/thread.hpp>

// class thread

// template <class F, class ...Args> thread(F f, Args... args);

#include <new>
#include <cstdlib>
#include <cassert>
#include <boost/thread/thread.hpp>
#include <boost/detail/lightweight_test.hpp>

unsigned throw_one = 0xFFFF;

#if defined _GLIBCXX_THROW
void* operator new(std::size_t s) _GLIBCXX_THROW (std::bad_alloc)
#elif defined BOOST_MSVC
void* operator new(std::size_t s)
#else
void* operator new(std::size_t s) throw (std::bad_alloc)
#endif
{
  std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  if (throw_one == 0) throw std::bad_alloc();
  --throw_one;
  return std::malloc(s);
}

#if defined BOOST_MSVC
void operator delete(void* p)
#else
void operator delete(void* p) throw ()
#endif
{
  std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  std::free(p);
}

bool f_run = false;

void f(int i, double j)
{
  BOOST_TEST(i == 5);
  BOOST_TEST(j == 5.5);
  f_run = true;
}

class G
{
  int alive_;
public:
  static int n_alive;
  static bool op_run;

  G() :
    alive_(1)
  {
    ++n_alive;
  }
  G(const G& g) :
    alive_(g.alive_)
  {
    ++n_alive;
  }
  ~G()
  {
    alive_ = 0;
    --n_alive;
  }

  void operator()(int i, double j)
  {
    BOOST_TEST(alive_ == 1);
    BOOST_TEST(n_alive >= 1);
    BOOST_TEST(i == 5);
    BOOST_TEST(j == 5.5);
    op_run = true;
  }
};

int G::n_alive = 0;
bool G::op_run = false;


int main()
{
  {
    std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
    boost::thread t(f, 5, 5.5);
    t.join();
    BOOST_TEST(f_run == true);
    std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
  }
#ifndef BOOST_MSVC
  f_run = false;
  {
    std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
    try
    {
      std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
      throw_one = 0;
      boost::thread t(f, 5, 5.5);
      BOOST_TEST(false);
      std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
    }
    catch (...)
    {
      std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
      throw_one = 0xFFFF;
      BOOST_TEST(!f_run);
      std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
    }
    std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
  }
#endif
  {
    std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
    BOOST_TEST(G::n_alive == 0);
    std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
    BOOST_TEST(!G::op_run);
    boost::thread t(G(), 5, 5.5);
    t.join();
    std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
    BOOST_TEST(G::n_alive == 0);
    BOOST_TEST(G::op_run);
    std::cout << __FILE__ << ":" << __LINE__ <<" " << G::n_alive << std::endl;
  }

  return boost::report_errors();
}