YouCompleteMe/cpp/BoostParts/boost/thread/detail/thread_group.hpp

155 lines
4.1 KiB
C++
Raw Normal View History

2012-05-10 00:45:30 -04:00
#ifndef BOOST_THREAD_DETAIL_THREAD_GROUP_HPP
#define BOOST_THREAD_DETAIL_THREAD_GROUP_HPP
// 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)
// (C) Copyright 2007-9 Anthony Williams
#include <list>
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread/mutex.hpp>
2013-02-13 23:03:52 -05:00
#include <boost/thread/lock_guard.hpp>
2012-05-10 00:45:30 -04:00
#include <boost/config/abi_prefix.hpp>
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4251)
#endif
namespace boost
{
class thread_group
{
private:
thread_group(thread_group const&);
2013-02-13 23:03:52 -05:00
thread_group& operator=(thread_group const&);
2012-05-10 00:45:30 -04:00
public:
thread_group() {}
~thread_group()
{
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
it!=end;
++it)
{
delete *it;
}
}
2013-02-13 23:03:52 -05:00
bool is_this_thread_in()
{
thread::id id = this_thread::get_id();
boost::shared_lock<shared_mutex> guard(m);
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
it!=end;
++it)
{
if ((*it)->get_id() == id)
return true;
}
return false;
}
bool is_thread_in(thread* thrd)
{
if(thrd)
{
thread::id id = thrd->get_id();
boost::shared_lock<shared_mutex> guard(m);
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
it!=end;
++it)
{
if ((*it)->get_id() == id)
return true;
}
return false;
}
else
{
return false;
}
}
2012-05-10 00:45:30 -04:00
template<typename F>
thread* create_thread(F threadfunc)
{
boost::lock_guard<shared_mutex> guard(m);
std::auto_ptr<thread> new_thread(new thread(threadfunc));
threads.push_back(new_thread.get());
return new_thread.release();
}
2013-02-13 23:03:52 -05:00
2012-05-10 00:45:30 -04:00
void add_thread(thread* thrd)
{
if(thrd)
{
2013-02-13 23:03:52 -05:00
BOOST_THREAD_ASSERT_PRECONDITION( ! is_thread_in(thrd) ,
thread_resource_error(system::errc::resource_deadlock_would_occur, "boost::thread_group: trying to add a duplicated thread")
);
2012-05-10 00:45:30 -04:00
boost::lock_guard<shared_mutex> guard(m);
threads.push_back(thrd);
}
}
2013-02-13 23:03:52 -05:00
2012-05-10 00:45:30 -04:00
void remove_thread(thread* thrd)
{
boost::lock_guard<shared_mutex> guard(m);
std::list<thread*>::iterator const it=std::find(threads.begin(),threads.end(),thrd);
if(it!=threads.end())
{
threads.erase(it);
}
}
2013-02-13 23:03:52 -05:00
2012-05-10 00:45:30 -04:00
void join_all()
{
2013-02-13 23:03:52 -05:00
BOOST_THREAD_ASSERT_PRECONDITION( ! is_this_thread_in() ,
thread_resource_error(system::errc::resource_deadlock_would_occur, "boost::thread_group: trying joining itself")
);
2012-05-10 00:45:30 -04:00
boost::shared_lock<shared_mutex> guard(m);
2013-02-13 23:03:52 -05:00
2012-05-10 00:45:30 -04:00
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
it!=end;
++it)
{
2013-02-13 23:03:52 -05:00
if ((*it)->joinable())
2012-05-10 00:45:30 -04:00
(*it)->join();
}
}
2013-02-13 23:03:52 -05:00
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
2012-05-10 00:45:30 -04:00
void interrupt_all()
{
boost::shared_lock<shared_mutex> guard(m);
2013-02-13 23:03:52 -05:00
2012-05-10 00:45:30 -04:00
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
it!=end;
++it)
{
(*it)->interrupt();
}
}
2013-02-13 23:03:52 -05:00
#endif
2012-05-10 00:45:30 -04:00
size_t size() const
{
boost::shared_lock<shared_mutex> guard(m);
return threads.size();
}
2013-02-13 23:03:52 -05:00
2012-05-10 00:45:30 -04:00
private:
std::list<thread*> threads;
mutable shared_mutex m;
};
}
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#include <boost/config/abi_suffix.hpp>
#endif