2
0
mirror of https://github.com/boostorg/thread.git synced 2026-02-20 15:12:11 +00:00

Threads: Added a lot of unit tests

[SVN r76295]
This commit is contained in:
Vicente J. Botet Escriba
2012-01-03 21:23:11 +00:00
parent 5a7545afbd
commit a4d9355060
91 changed files with 5283 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
//===----------------------------------------------------------------------===//
//
// 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
// void join();
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
#include <new>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <boost/detail/lightweight_test.hpp>
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()()
{
BOOST_TEST(alive_ == 1);
BOOST_TEST(n_alive == 1);
op_run = true;
}
};
int G::n_alive = 0;
bool G::op_run = false;
boost::thread* resource_deadlock_would_occur_th;
boost::mutex resource_deadlock_would_occur_mtx;
void resource_deadlock_would_occur_tester()
{
try
{
boost::unique_lock<boost::mutex> lk(resource_deadlock_would_occur_mtx);
resource_deadlock_would_occur_th->join();
BOOST_TEST(false);
}
catch (boost::system::system_error& e)
{
BOOST_TEST(e.code().value() == boost::system::errc::resource_deadlock_would_occur);
}
}
int main()
{
{
boost::thread t0( (G()));
BOOST_TEST(t0.joinable());
t0.join();
BOOST_TEST(!t0.joinable());
}
{
boost::unique_lock<boost::mutex> lk(resource_deadlock_would_occur_mtx);
boost::thread t0( resource_deadlock_would_occur_tester );
resource_deadlock_would_occur_th = &t0;
BOOST_TEST(t0.joinable());
lk.unlock();
t0.join();
BOOST_TEST(!t0.joinable());
}
// {
// boost::thread t0( (G()));
// t0.detach();
// try
// {
// t0.join();
// BOOST_TEST(false);
// }
// catch (boost::system::system_error& e)
// {
// BOOST_TEST(e.code().value() == boost::system::errc::no_such_process);
// }
// }
// {
// boost::thread t0( (G()));
// BOOST_TEST(t0.joinable());
// t0.join();
// BOOST_TEST(!t0.joinable());
// try
// {
// t0.join();
// BOOST_TEST(false);
// }
// catch (boost::system::system_error& e)
// {
// BOOST_TEST(e.code().value() == boost::system::errc::invalid_argument);
// }
//
// }
return boost::report_errors();
}