~ubuntu-branches/ubuntu/wily/davix/wily

« back to all changes in this revision

Viewing changes to deps/boost_intern/src/thread/test/sync/conditions/condition_variable_any/wait_for_pred_pass.cpp

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2015-07-31 13:17:55 UTC
  • mfrom: (5.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20150731131755-mizprbmn7ogv33te
Tags: 0.4.1-1
* Update to version 0.4.1
* Implement Multi-Arch support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//===----------------------------------------------------------------------===//
2
 
//
3
 
//                     The LLVM Compiler Infrastructure
4
 
//
5
 
// This file is dual licensed under the MIT and the University of Illinois Open
6
 
// Source Licenses. See LICENSE.TXT for details.
7
 
//
8
 
//===----------------------------------------------------------------------===//
9
 
// Copyright (C) 2011 Vicente J. Botet Escriba
10
 
//
11
 
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
12
 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13
 
 
14
 
// <boost/thread/condition_variable_any>
15
 
 
16
 
// class condition_variable_any;
17
 
 
18
 
// condition_variable_any(const condition_variable_any&) = delete;
19
 
 
20
 
#include <boost/thread/condition_variable.hpp>
21
 
#include <boost/thread/mutex.hpp>
22
 
#include <boost/thread/thread.hpp>
23
 
#include <boost/detail/lightweight_test.hpp>
24
 
 
25
 
#if defined BOOST_THREAD_USES_CHRONO
26
 
 
27
 
class Pred
28
 
{
29
 
  int& i_;
30
 
public:
31
 
  explicit Pred(int& i) :
32
 
    i_(i)
33
 
  {
34
 
  }
35
 
 
36
 
  bool operator()()
37
 
  {
38
 
    return i_ != 0;
39
 
  }
40
 
};
41
 
 
42
 
boost::condition_variable_any cv;
43
 
 
44
 
typedef boost::timed_mutex L0;
45
 
typedef boost::unique_lock<L0> L1;
46
 
 
47
 
L0 m0;
48
 
 
49
 
int test1 = 0;
50
 
int test2 = 0;
51
 
 
52
 
int runs = 0;
53
 
 
54
 
void f()
55
 
{
56
 
  typedef boost::chrono::system_clock Clock;
57
 
  typedef boost::chrono::milliseconds milliseconds;
58
 
  L1 lk(m0);
59
 
  BOOST_TEST(test2 == 0);
60
 
  test1 = 1;
61
 
  cv.notify_one();
62
 
  Clock::time_point t0 = Clock::now();
63
 
  //bool r =
64
 
      (void)cv.wait_for(lk, milliseconds(250), Pred(test2));
65
 
  Clock::time_point t1 = Clock::now();
66
 
  if (runs == 0)
67
 
  {
68
 
    BOOST_TEST(t1 - t0 < milliseconds(250));
69
 
    BOOST_TEST(test2 != 0);
70
 
  }
71
 
  else
72
 
  {
73
 
    BOOST_TEST(t1 - t0 - milliseconds(250) < milliseconds(250+5));
74
 
    BOOST_TEST(test2 == 0);
75
 
  }
76
 
  ++runs;
77
 
}
78
 
 
79
 
int main()
80
 
{
81
 
  {
82
 
    L1 lk(m0);
83
 
    boost::thread t(f);
84
 
    BOOST_TEST(test1 == 0);
85
 
    while (test1 == 0)
86
 
      cv.wait(lk);
87
 
    BOOST_TEST(test1 != 0);
88
 
    test2 = 1;
89
 
    lk.unlock();
90
 
    cv.notify_one();
91
 
    t.join();
92
 
  }
93
 
  test1 = 0;
94
 
  test2 = 0;
95
 
  {
96
 
    L1 lk(m0);
97
 
    boost::thread t(f);
98
 
    BOOST_TEST(test1 == 0);
99
 
    while (test1 == 0)
100
 
      cv.wait(lk);
101
 
    BOOST_TEST(test1 != 0);
102
 
    lk.unlock();
103
 
    t.join();
104
 
  }
105
 
 
106
 
  return boost::report_errors();
107
 
}
108
 
 
109
 
#else
110
 
#error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
111
 
#endif